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

8 Responses to “using kill to see if a process is alive”

  1. Brock Noland Says:

    That’s it, I think I am changing to LaTex.

  2. Wade Says:

    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

  3. Brock Noland Says:

    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

  4. Wade Says:

    Well either way, I’m glad you brought it up. Thanks for the great tip!

  5. John Says:

    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.

  6. Brock Noland Says:

    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.”

  7. Shadow Says:

    Норм

  8. SuperMan Says:

    Спс

Leave a Reply

If Wordpress eats your comment (shell output, loops, ex..) email the text to me.