I have two command line programs, a client application and a server application it talks to.
The client depends on the server running in the background. I can launch both via:
java -jar server.jar & java -jar client.jar
When I kill the client, however, the server remains in the background. Is there a way to link the two so that if the client dies, the server dies?
Run the server job in the background and store process id using $!. Then run the client.
After the client exits, kill the server using the stored pid, like this:
java -jar server.jar &
server=$!
java -jar client.jar
kill $server
shorter: no need to store PID of the background process, there's only one
java -jar server.jar &
java -jar client.jar
kill $!
There is no way to link them, but you can manage the processes explicitly.
java -jar server.jar & server_pid=$!
java -jar client.jar
kill $server_pid
The server runs in the background, then the client runs in the foreground. The script blocks at this point, so when the client exits, the script proceeds with the next command, which kills the server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With