I am trying to make a bash script to start a jar file and do it in the background. For that reason I'm using nohup
. Right now I can capture the pid of the java process but I also need to be able to execute a command when the process finishes.
This is how I started
nohup java -jar jarfile.jar & echo $! > conf/pid
I also know from this answer that using ;
will make a command execute after the first one finishes.
nohup java -jar jarfile.jar; echo "done"
echo "done"
is just an example. My problem now is that I don't know how to combine them both. If I run echo $!
first then echo "done"
executes immediately. While if echo "done"
goes first then echo $!
will capture the PID of echo "done"
instead of the one of the jarfile.
I know that I could achieve the desire functionality by polling until I don't see the PID running anymore. But I would like to avoid that as much as possible.
You can use the bash util
wait
once you start the process usingnohup
I don't think you're going to get around "polling until you don't see the pid running anymore."
wait
is a bash builtin; it's what you want and I'm certain that's exactly what it does behind the scenes. But since Inian beat me to it, here's a friendly function for you anyway (in case you want to get a few things running in parallel).Running a command like this will give the desired effect and suppress unneeded job output: