The remote port occupation of Linux startup jar package script reports an error

PATH=$PATH:$JAVA_HOME/bin
NAME=$1
VERSION=$2
PORT=$3
echo $NAME
ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
for id in $ID
do
kill -9 $id
echo "killed $id"
done
nohup java -server -Xms600m -Xmx600m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -Xverify:none -XX:+DisableExplicitGC 
-Djava.awt.headless=true -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=$PORT,suspend=n -Duser.timezone=Asia/Shanghai 
-Denv=fat -Dapollo.cluster=vmfat03 -jar $NAME-$VERSION.jar  > $NAME.log 2>&1 &

  The script is shown in the figure, which is a very common one. However, if I add a remote port when I start, an error will be reported when I run the script next time

ERROR: transport error 202: bind failed: Address already in use
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [debugInit.c:750]

The remote port is occupied, but I have obviously killed the process, and the command query has also been killed.

After guessing whether it will take some time for the remote connection port to close completely after killing the process, but at this time, you use the same port number to start the remote debug mode, which will lead to the error shown in the figure above, so I’ll sleep 1 behind the done in the script. Let him wait 1s.

The result proved that the conjecture was correct, and then no such error was reported

Read More: