What does `kill -0 $pid` in a shell script do?

BashShellScriptingSignalsKill

Bash Problem Overview


Basically, what signal does '0' represent, because here I see SIGNAL numbers starting from 1.

Bash Solutions


Solution 1 - Bash

sending the signal 0 to a given PID just checks if any process with the given PID is running and you have the permission to send a signal to it.

For more information see the following manpages:

kill(1)
$ man 1 kill
...
If sig is 0, then no signal is sent, but error checking is still performed.
...
kill(2)
$ man 2 kill
...
If sig is 0, then no signal is sent, but error checking is still performed; this 
can be used to check for the existence of a process ID or process group ID.
...

Solution 2 - Bash

This is a Good Question Because...

...it can be hard to find documentation on this special signal. Despite what others have said, the only mention of this signal in man 1 kill in Debian-based systems is:

> Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0.

Not especially helpful, especially if you don't already know what the signal does. It is also not listed by the output of kill -l, so you won't know about it unless you already know about it.

Where to Find It Documented

On Debian and Ubuntu systems, the output of man 2 kill says, in part:

> If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.

What It's Good For

You can use kill -0 to check whether a process is running. Consider these examples.

# Kill the process if it exists and accepts signals from
# the current user.
sleep 60 &
pid=$!
kill -0 $pid && kill $pid

# Check if a PID exists. When missing, this should result
# in output similar to:
#    bash: kill: (6228) - No such process
#    Exit status: 1
kill -0 $pid; echo "Exit status: $?"

You can also use kill -0 to determine if the current user has permissions to signal a given process. For example:

# See if you have permission to signal the process. If not,
# this should result in output similar to:
#     bash: kill: (15764) - Operation not permitted
#     Exit status: 1
sudo sleep 60 &
kill -0 $!; echo "Exit status: $?"

Solution 3 - Bash

kill -0 $pid is to check whether the process with process id (pid) exists or not.

Be careful while using kill -0 $pid to check the process existence because

  1. Once the intended process exit then its pid can be allot to other newly created process. ( So one can not be so sure that particular process is alive or not )

  2. In case of zombie process, for which child is waiting for parent to call wait. Here it hold the $pid and give the positive result while that process is not running.

Solution 4 - Bash

This command checks wether the process with PID in $pid is alive.

Solution 5 - Bash

kill -0 $pid is used to check if a process running with $pid is alive or not. But this can be tricky, as process ID can be reassigned, once a process exit and new process runs.

One can use killall -0 <process name> to get about a particular process is running or not.

Solution 6 - Bash

Sending the EXIT signal, or 0 to a process will:

  1. Check for the existence of a process.
  2. Do various error checking on the process (PID, PGID, etc ...).
  3. It will not send any output to stdout upon success.
  4. Send an error message to stderr if something is not right.
  5. Give you a false positive if the process is defunct (i.e. Zombie).

More explicitly, a useful function for your shell scripts would be:

function isProcess ()
{
    kill -s EXIT $1 2> /dev/null
}

This returns no text to stdout upon success, but an error message to stderr upon failure (but I have redirected that error message to /dev/null).

If you are concerned about defunct / zombie process status, then you need to use ps, preferably with the --no-headers switch.

#!/bin/ksh

function trim ()
{
    echo -n "$1" | tr -d [:space:]
}

function getProcessStatus ()
{
    trim $(ps -p $1 -o stat --no-headers)
}

function isZombie ()
{
    typeset processStatus=$(getProcessStatus $1)
    
    [[ "$processStatus" == "Z" ]]
    return $?
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongjainView Question on Stackoverflow
Solution 1 - BashdwalterView Answer on Stackoverflow
Solution 2 - BashTodd A. JacobsView Answer on Stackoverflow
Solution 3 - BashSandeep_blackView Answer on Stackoverflow
Solution 4 - BashFritz G. MehnerView Answer on Stackoverflow
Solution 5 - Bashuser9344710View Answer on Stackoverflow
Solution 6 - BashAnthony RutledgeView Answer on Stackoverflow