How to check whether the process ID still exists

Not well documented, but a quick and simple operation.

Try to send signal 0 to the specified process ID using kill command, then exit code will indicate whether signal may be sent.

The following example shows existing process ID.

$ pgrep firefox
$ kill -0 4677
$ echo $?

The following example shows non-existing process ID.

$ kill -0 999999899
bash: kill: (999999899) - No such process
$ echo $?

The following shell script is the final example, as it will simply kill process using PID stored in the /var/run/ file, if the process still exists.


kill -0 $(cat $pid_file) 2>/dev/null

if [ $? -eq 0 ]; then
  kill $(cat $pid_file)

I do not understand why this is currently not mentioned in the kill manual page, but it was there [check procps-ng source code] some time ago.

The signals listed below may be available for use with kill.
When known constant, numbers and default behavior are shown.

lB rB lB lB
lfCW r l l.
Name	Num	Action	Description
0	0	n/a	exit code indicates if a signal may be sent
ALRM	14	exit
HUP	1	exit
INT	2	exit
KILL	9	exit	this signal may not be blocked
PIPE	13	exit
POLL		exit
PROF		exit
TERM	15	exit
USR1		exit
USR2		exit
VTALRM		exit
STKFLT		exit	may not be implemented
PWR		ignore	may exit on some systems
WINCH		ignore
CHLD		ignore
URG		ignore
TSTP		stop	may interact with the shell
TTIN		stop	may interact with the shell
TTOU		stop	may interact with the shell
STOP		stop	this signal may not be blocked
CONT		restart	continue if stopped, otherwise ignore
ABRT	6	core
FPE	8	core
ILL	4	core
QUIT	3	core
SEGV	11	core
TRAP	5	core
SYS		core	may not be implemented
EMT		core	may not be implemented
BUS		core	core dump may fail
XCPU		core	core dump may fail
XFSZ		core	core dump may fail
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.