using kill to see if a process is alive
April 9th, 2008
I am making some changes to the moreutils sponge command. Sponge provides a method of prepending which is less specialized than my prepend util. However, it has trouble with large amounts of input.
Regardless, while testing my changes, I want to watch it operate. Normally, you would just do so from a second terminal. That is a pain. kill -0 can be very useful for this. After backgrounding the command, I assign the pid (via the variable $!) to $pid using eval. eval is needed to stop BASH from expanding $! until after the background operation.
After that, I enter a while loop on kill -0 $pid, which will not kill $pid, but will return successfully until $pid has died:
# cat large-file-GB | ./sponge large-file-GB-copy & eval 'pid=$!'; while kill -0 $pid; do sleep 10; ls -lh large-file* /tmp/sponge.*; echo;done [1] 7937 -rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB -rw------- 1 root root 128M 2008-04-09 17:23 /tmp/sponge.JMsBWG -rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB -rw------- 1 root root 384M 2008-04-09 17:23 /tmp/sponge.JMsBWG -rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB -rw------- 1 root root 877M 2008-04-09 17:24 /tmp/sponge.JMsBWG -rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB -rw-r--r-- 1 root root 20M 2008-04-09 17:24 large-file-GB-copy -rw------- 1 root root 896M 2008-04-09 17:24 /tmp/sponge.JMsBWG -rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB -rw-r--r-- 1 root root 413M 2008-04-09 17:25 large-file-GB-copy -rw------- 1 root root 896M 2008-04-09 17:24 /tmp/sponge.JMsBWG -rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB -rw-r--r-- 1 root root 836M 2008-04-09 17:25 large-file-GB-copy -rw------- 1 root root 896M 2008-04-09 17:24 /tmp/sponge.JMsBWG -rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB -rw-r--r-- 1 root root 920M 2008-04-09 17:25 large-file-GB-copy [1]+ Done cat large-file-GB | ./sponge large-file-GB-copy ls: cannot access /tmp/sponge.*: No such file or directory -rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB -rw-r--r-- 1 root root 977M 2008-04-09 17:25 large-file-GB-copy -bash: kill: (7937) - No such process # md5sum large-file-GB* b5c667a723a10a3485a33263c4c2b978 large-file-GB b5c667a723a10a3485a33263c4c2b978 large-file-GB-copy


April 9th, 2008 at 11:45 pm
That’s it, I think I am changing to LaTex.
April 10th, 2008 at 11:26 am
Brock –
I’m curious how you discovered the -0 for kill. I’ve never seen that in “kill -l”, kill(1), or signal(7)! Did you happen to find it by looking through the code, or by accident, or is it just something that I should know already?
Wade
April 10th, 2008 at 11:36 am
Wade,
Thanks for your comment. Its there, but you wouldn’t know it unless you were looking for it:
Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0.
Further down, it says this regarding 0:
exit code indicates if a signal may be sent
As such, it exits successfully if you may send an exit code, ie the process is alive.
I believe I read this in Advanced UNIX Programming, but I am not exactly sure. Its possible I read in the coreutils kill source.
Brock
April 10th, 2008 at 12:34 pm
Well either way, I’m glad you brought it up. Thanks for the great tip!
February 21st, 2009 at 7:39 pm
Not exactly correct. A process can be alive without you being able to send a signal to it.
If you are not the process own, process group owner, or root, then you cannot send a signal to it.
November 9th, 2009 at 1:38 am
What isn’t exactly correct?
I think is quite correct:
“exit code indicates if a signal may be sent
As such, it exits successfully if you may send an exit code, ie the process is alive.”
January 9th, 2010 at 2:55 pm
…
Норм …
January 12th, 2010 at 4:58 am
…
Спс …