Search code examples
linuxunixshellbackground-process

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


I tried running a script using nohup like,

nohup script.sh &

When I tried

ps -ef | grep "script.sh"

I couldn't find it there except for the grep which is being run with that string as a parameter.

Am I doing it right?. Does this mean that the process has indeed finished execution? Thanks.


Solution

  • 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