Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: )

Send a 0 (zero) signal to the process ID you want to check using the kill function. If the process exists, the function returns true, otherwise it returns false.

Example:

#-- check if process 1525 is running
$exists = kill 0, 1525;
print "Process is running\n" if ( $exists );


used howto to monitor and restart application

Struggled with tearing apart ps output which was clunky, then found your elegant suggestion of signal 0 which returns "truth" of whether the process is running or not.

Used the technique to monitor bind on OpenBSD and restart if it isn't running.
Script can be run manually and reports if process running or not.
Script normally run from cron (i use 1-minute intervals).

ns1:named {146} cat named-test
#!/usr/bin/perl
#
# test to see if named process exists and if not, start it up again.
#
# (named writes process number on first line followed by the command used
# to start it in second line of /var/named/named.pid)
# cat named.pid
# 6389
# named -t /var/named -u named
#
# test to see if named process exists and if not, start it up again.
#
$exists = kill 0, `head -1 /var/named/named.pid`;
#
unless ($exists) { exec `tail -1 /var/named/named.pid` }
#
print "\n\$exists = <$exists>\n";