= Waiting On Another Process = == Problem == I want to run a process after another process completes. == Solution == Calling {{{kill -0 PID}}} will exit with either 0 for success (the process exists '''and''' this user may send a signal to it) or 1 otherwise. On STDERR an error string will indicate the cause. '''Note:''' this does not work if you do not have permission to send a signal to the process (ie it's owned by another user). To do this for a process owned by a different user root access is needed. So use a simple loop - I'm waiting for a process with process-id {{{PID}}} and then want to run {{{/path/to/my/command}}}: {{{ notroot@host:~$ while kill -0 PID ; do echo -n . ; sleep 5 ; done ; /path/to/my/command }}} == Examples == === waiting for my process === {{{ notroot@host:~$ sleep 30 & [1] 10567 notroot@host:~$ while kill -0 10567 ; do echo -n . ; sleep 2; done; echo "done at `date`" ...........[1]+ Done sleep 30 bash: kill: (10567) - No such process done at Wed Feb 19 15:36:10 EST 2014 }}} === waiting for a process owned by another user === This requires sudo access (or to be running the loop as root) - the change in the example is to run sleep as another user and to call kill with sudo. {{{ otheruser@host:~$ sleep 30 & [1] 11483 notroot@host:~$ while sudo kill -0 11483 ; do echo -n . ; sleep 2; done; echo "done at `date`" .........kill: No such process done at Wed Feb 19 15:43:30 EST 2014 }}} === kill -0 examples === A process which exists, but this user does not have access to it: {{{ notroot@host:~$ kill -0 1 bash: kill: (1) - Operation not permitted notroot@host:~$ echo $? 1 }}} A process which does not exist: {{{ notroot@host:~$ kill -0 234123 bash: kill: (234123) - No such process notroot@host:~$ echo $? 1 }}} An invalid argument: {{{ notroot@host:~$ kill -0 sdfasd bash: kill: sdfasd: arguments must be process or job IDs notroot@host:~$ echo $? 1 }}} But here, we have access (now root) and the process exists: {{{ notroot@host:~$ /usr/bin/sudo kill -0 1 notroot@host:~$ echo $? 0 }}} And here we have access to the process because it's owned by the user: {{{ notroot@host:~$ sleep 10 & [1] 8966 notroot@host:~$ kill -0 8966 notroot@host:~$ echo $? 0 notroot@host:~$ ... ... [1]+ Done sleep 10 notroot@host:~$ kill -0 8966 bash: kill: (8966) - No such process notroot@host:~$ echo $? 1 }}}