150

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

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
gjain
  • 4,468
  • 5
  • 39
  • 47
  • 3
    http://linux.die.net/man/1/kill – Lee Jun 13 '12 at 10:04
  • 3
    See also the [Unix.SE variant of this question](https://unix.stackexchange.com/questions/169898/what-does-kill-0-do "What does \`kill -0\` do?"), which *differs* from `kill 0` (no dash), explained [here](https://unix.stackexchange.com/questions/67532/what-does-kill-0-do-actually "What does kill 0 do actually?") and [here](https://superuser.com/questions/563972/what-does-kill-0-do-actually "What does kill 0 do actually?"). – Adam Katz Jan 04 '17 at 22:47

6 Answers6

146

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.
...
slm
  • 15,396
  • 12
  • 109
  • 124
dwalter
  • 7,258
  • 1
  • 32
  • 34
  • 7
    The location of this information (if it exists at all) is highly system-dependent. On recent Debian-based systems, use `man 2 kill` instead. – Todd A. Jacobs Jan 12 '13 at 18:15
  • 2
    Both `man 1 kill` and `man 2 kill` had it on my Fedora 20 system. It's hard to spot though, buried in both those man pages. – slm Nov 21 '14 at 22:15
  • Or rely on the posix manual instead: ```If sig is 0 (the null signal), error checking is performed but no signal is actually sent. The null signal can be used to check the validity of pid.``` http://pubs.opengroup.org/onlinepubs/009695399/functions/kill.html – taddy hoops May 09 '17 at 04:53
  • 2
    I feel like the command `man 2 kill` is outside the 1st amendment :) – JBaczuk Aug 24 '17 at 13:49
134

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: $?"
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • On mac and BSD it's also documented in `kill(2)` Here's the snippet: `The kill() function sends the signal specified by sig to pid, a process or a group of processes. Typically, Sig will be one of the signals specified in sigaction(2). A value of 0, however, will cause error checking to be performed (with no signal being sent). This can be used to check the validity of pid.` – lukecampbell May 05 '14 at 14:44
  • 8
    This should be the accepted answer. Much better than the other one. the documentation on signal 0 is tough to locate. It's buried in the `kill` man page: *"If sig is 0, then no signal is sent, but error checking is still performed."* – slm Nov 21 '14 at 22:10
10

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Sandeep_black
  • 1,352
  • 17
  • 18
6

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

Fritz G. Mehner
  • 16,550
  • 2
  • 34
  • 41
  • 1
    The man page says: "If sig is 0, then no signal is sent, but error checking is still performed." What error checking are we referring to here? – gjain Jun 13 '12 at 10:14
  • 2
    -1, since a process with PID `$pid` may be running but you don't have the permission to send a signal to it. – dwalter Jun 13 '12 at 10:16
  • 3
    @dwalter: If you do not have permission you will get an EPERM. If it does not exist, you will get an ESRCH. The `kill(1)` will print a different error for each. So, you can tell if the pid is alive regardless of whether you have permissions to send signals or not. Furthermore, the typical usage of `kill -0` is to see if the pid is alive, even if it is not always used correctly. I'd say this answer is correct (apart from the spelling). – camh Jun 13 '12 at 10:43
  • 3
    @camh: no the return value of `kill -0 $pid` will be the same in both cases. It will return `1` so you cannot say without parsing the ouput of `kill` if the process is running or not, if you don't have the permission to send a signal to it. EDIT: yes I know it is used most of the time for checking if a process is alive, but this is wrong unless you can guarantee that you have the permission to send the signal (eg: being root) – dwalter Jun 13 '12 at 10:45
  • 1
    @dawlter: So? The question did not specify how the command was called, so parsing output is a valid way to determine the outcome of the command. – camh Jun 13 '12 at 10:46
  • 1
    @camh: even the OP asked for clarification, see 1st comment after answer. – dwalter Jun 13 '12 at 10:50
  • 2
    @dwalter: My point was that the answer is correct. You tried to be pedantic and point out that there was another error case, but I am telling you that technically the answer covers that case too since the `kill` bash built-in (the question is tagged `bash`) outputs the type of error on stderr, and indication of an error in its return code. That is, "This command checks [whether] the process with PID in $pid is alive" is completely correct if you interpret the output correctly. [I would not have commented if you did not say you gave -1 to the answer. Your comment is otherwise valid]. – camh Jun 13 '12 at 11:00
1

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
1

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 $?
}
Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44