Home » What does `kill -0` do?

What does `kill -0` do?


This one is a little hard to glean but if you look in the following 2 man pages you’ll see the following notes:


$ man 1 kill
If sig is 0, then no signal is sent, but error checking is still performed.


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

So signal 0 will not actually in fact send anything to your process’s PID, but will check whether you have permissions to do so.

Where might this be useful?

One obvious place would be if you were trying to determine if you had permissions to send signals to a running process via kill. You could check prior to sending the actual kill signal that you want, by wrapping a check to make sure that kill -0 <PID> was first allowed.


Say a process was being run by root as follows:

$ sudo sleep 2500 &
[1] 15693

Now in another window if we run this command we can confirm that that PID is running.

$ pgrep sleep

Now let’s try this command to see if we have access to send that PID signals via kill.

$ if ! kill -0 $(pgrep sleep); then echo "You're weak!"; fi
bash: kill: (15693) - Operation not permitted
You're weak!

So it works, but the output is leaking a message from the kill command that we don’t have permissions. Not a big deal, simply catch STDERR and send it to /dev/null.

$ if ! kill -0 $(pgrep sleep) 2>/dev/null; then echo "You're weak!"; fi
You're weak!

Complete example

So then we could do something like this, killer.bash:


PID=$(pgrep sleep)
if ! kill -0 $PID 2>/dev/null; then 
  echo "you don't have permissions to kill PID:$PID"
  exit 1

kill -9 $PID

Now when I run the above as a non-root user:

$ ~/killer.bash 
you don't have permissions to kill PID:15693

$ echo $?

However when it’s run as root:

$ sudo ~/killer.bash 

$ echo $?

$ pgrep sleep

kill -0 (or its more portable POSIX variant kill -s 0) goes through the motion of sending a signal, but doesn’t actually send one. It’s a feature of the underlying C API that the shell command exposes in a straightforward way.

kill -s 0 -- "$pid" thus tests whether there is a running process with the given PID (or PGID if $pid is negative), and whether the current process would have the permission to send it (any of the processes in the process group in case of a negative $pid) a signal. It’s mostly a way to test whether a process (or process group) is alive.

Keep in mind that even if there is a running process with the expected PID and permissions, this is not necessarily the process that you expect. It’s possible that the process that you expect died earlier and its PID was reused for an unrelated process. The right way to monitor processes is by letting their parent do it — a process’s PID is not reused until its parent has acknowledged its death (that’s why zombies exist), so a process’s parent can reliably identify its children by their PID.

The kill -0 $pid tells you if a process with $pid exists.

In the snippet

if ! kill -0 $(cat /path/to/file.pid); then
    ... do something ...

the block ... do something ... is executed if a process with the PID that is stored in /path/to/file.pid is running – and – unless the snippet runs as root – if the PID runs under the same user.

The POSIX standard specifies the role of the 0 signal:

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.

(kill(3p), POSIX.1-2008 – similar wording in POSIX.1-2001)

Note that POSIX specifies both kill -0 and kill -s 0 command line styles (kill(1p)).

In contrast to the kill syscall interface the kill command can’t be used to reliably check for the existence of PIDs owned by other users (as a normal user), e.g.:

$ kill -0 123
kill: kill 123 failed: no such process
$ echo $?


$ kill -0 1
kill: kill 1 failed: operation not permitted
$ echo $?

When calling the kill syscall one can reliably distinguish these cases via looking at the errno value (cf. e.g. an Python Example).

Related Solutions

Joining bash arguments into single string with spaces

[*] I believe that this does what you want. It will put all the arguments in one string, separated by spaces, with single quotes around all: str="'$*'" $* produces all the scripts arguments separated by the first character of $IFS which, by default, is a space....

AddTransient, AddScoped and AddSingleton Services Differences

TL;DR Transient objects are always different; a new instance is provided to every controller and every service. Scoped objects are the same within a request, but different across different requests. Singleton objects are the same for every object and every...

How to download package not install it with apt-get command?

Use --download-only: sudo apt-get install --download-only pppoe This will download pppoe and any dependencies you need, and place them in /var/cache/apt/archives. That way a subsequent apt-get install pppoe will be able to complete without any extra downloads....

What defines the maximum size for a command single argument?

Answers Definitely not a bug. The parameter which defines the maximum size for one argument is MAX_ARG_STRLEN. There is no documentation for this parameter other than the comments in binfmts.h: /* * These are the maximum length and maximum number of strings...

Bulk rename, change prefix

I'd say the simplest it to just use the rename command which is common on many Linux distributions. There are two common versions of this command so check its man page to find which one you have: ## rename from Perl (common in Debian systems -- Ubuntu, Mint,...

Output from ls has newlines but displays on a single line. Why?

When you pipe the output, ls acts differently. This fact is hidden away in the info documentation: If standard output is a terminal, the output is in columns (sorted vertically) and control characters are output as question marks; otherwise, the output is...

mv: Move file only if destination does not exist

mv -vn file1 file2. This command will do what you want. You can skip -v if you want. -v makes it verbose - mv will tell you that it moved file if it moves it(useful, since there is possibility that file will not be moved) -n moves only if file2 does not exist....

Is it possible to store and query JSON in SQLite?

SQLite 3.9 introduced a new extension (JSON1) that allows you to easily work with JSON data . Also, it introduced support for indexes on expressions, which (in my understanding) should allow you to define indexes on your JSON data as well. PostgreSQL has some...

Combining tail && journalctl

You could use: journalctl -u service-name -f -f, --follow Show only the most recent journal entries, and continuously print new entries as they are appended to the journal. Here I've added "service-name" to distinguish this answer from others; you substitute...

how can shellshock be exploited over SSH?

One example where this can be exploited is on servers with an authorized_keys forced command. When adding an entry to ~/.ssh/authorized_keys, you can prefix the line with command="foo" to force foo to be run any time that ssh public key is used. With this...

Why doesn’t the tilde (~) expand inside double quotes?

The reason, because inside double quotes, tilde ~ has no special meaning, it's treated as literal. POSIX defines Double-Quotes as: Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the...

What is GNU Info for?

GNU Info was designed to offer documentation that was comprehensive, hyperlinked, and possible to output to multiple formats. Man pages were available, and they were great at providing printed output. However, they were designed such that each man page had a...

Set systemd service to execute after fstab mount

a CIFS network location is mounted via /etc/fstab to /mnt/ on boot-up. No, it is not. Get this right, and the rest falls into place naturally. The mount is handled by a (generated) systemd mount unit that will be named something like mnt-wibble.mount. You can...

Merge two video clips into one, placing them next to each other

To be honest, using the accepted answer resulted in a lot of dropped frames for me. However, using the hstack filter_complex produced perfectly fluid output: ffmpeg -i left.mp4 -i right.mp4 -filter_complex hstack output.mp4 ffmpeg -i input1.mp4 -i input2.mp4...

How portable are /dev/stdin, /dev/stdout and /dev/stderr?

It's been available on Linux back into its prehistory. It is not POSIX, although many actual shells (including AT&T ksh and bash) will simulate it if it's not present in the OS; note that this simulation only works at the shell level (i.e. redirection or...

How can I increase the number of inodes in an ext4 filesystem?

It seems that you have a lot more files than normal expectation. I don't know whether there is a solution to change the inode table size dynamically. I'm afraid that you need to back-up your data, and create new filesystem, and restore your data. To create new...

Why doesn’t cp have a progress bar like wget?

The tradition in unix tools is to display messages only if something goes wrong. I think this is both for design and practical reasons. The design is intended to make it obvious when something goes wrong: you get an error message, and it's not drowned in...