In UNIX, signal 0
can be used with kill
command to check if a process exists and accepts signals from the current user. However, no signal is sent to the process when kill -0
is run. This command could be used as an alternative to parsing ps
output for checking the process existence. Since it’s not shown in most of the help pages or signal list via kill -l
, it may not be known to some users.
Examples: You can use the following code to check if a process exists.
The exit status of this command will be ‘0’ if the process with given pid exists, ‘1’ otherwise. errno
will be set appropriately if the command fails.
If you are unsure if a process is still running but you want to kill it if it runs, then you can use the below command.
For more info, check kill(2)