Wednesday, July 16, 2008

PERL script to check if a process is alive

Below i have a perl script that checks if a given process (passed as command-line argument) is alive or not. If the process had died for some reason, it re-invokes it.

Put this in crontab & schedule it to run every 1 hour or so.
This will take to keep your ever running processes alive.

The script:

#!/usr/bin/perl


my $process_to_check = $ARGV[0] or die "Usage: $0 \n";

open(PS,"/bin/ps -aef|") || die "Can't Open PS: $!\n";

while() {
chomp;
if (/\Q$process_to_check\E/) { close PS; exit;}
}

close PS;
system("$process_to_check");

0 comments: