how to find whether a script run as a nohup finished or not? how to find whether a script run as a nohup finished or not? linux linux

how to find whether a script run as a nohup finished or not?


At the beginning of your shell script, write the PID to a file (for example, in /var/run). Then, you can just search for that PID to know if the process is done or not. You can get the PID of your shell script using the built-in $$ variable.

To record the PID, put at the top of your script:

echo $$ > /var/run/myscript.pid

Then, to check if it's still running:

ps -p `cat /var/run/myscript.pid`

You might not be able to write into /var/run as a normal user. If not, just use /tmp


Subject to nohup implementation, but in most cases it will work.After running

nohup script.sh &

store the PID into a variable. $! is the PID of the last background process.

HISPID=$!

Then you can check if it's there with ps or kill:

ps -p $HISPIDkill -0 $HISPID

Unlike the other solution posted, this does not require modifying the script.sh


I followed the suggestion by scompt.com modifying my script to store the pid

Then I noticed the pid is written to the output, so there is no need to store it:

$ nohup ./sync-all.production.sh > sync-all.production.log &[1] 3428