I have a program .sh that spawns several child proces like so:
javac *.java
rmiregistry 3300 &
sleep 5
java Node 3300 3001 1 &
sleep 1
java Node 3300 3002 1 &
sleep 1
java Node 3300 3003 1 &
sleep 1
java Node 3300 3004 1 &
sleep 1
java Node 3300 3005 1 &
Now When I run this in a terminal screen I would like to be able to do something like ctr-c to stop this. However when I do it does not release the ports taken by the Nodes. Is there any way to make sure that when I ctr-c the ports are unbound or is there a shortkey that also frees up the ports used by the child processes? Or do I have to use a separate kill command? Also note that the nodes can and do launch multiple threads themselves (from within java).
The way to kill all the jobs launched by a process is to use the process group ID (pgid). The simplest way to see that it to launch your script in the background:
The number printed above is the PID of the
launch_java.sh
which is also the group ID since that launches the rest. Alternatively, you can find it throughps
:The 1st field is the pgid. Once you have that, you can use
kill
to kill it and all its children:As explained in
man kill
:use
ps -aef
orps -aux
command to find PID of parent of children and kill it bykill -9 PID
.