Click to See Complete Forum and Search --> : Use Perl to see if a process is running?


halldorr
01-13-2009, 02:06 PM
We're looking at ways to recover from server outages etc so we were looking at process IDs as a possibility. For example, we'd run a Perl script and get the process ID then store that in the database with the job.

What I'm not sure of is if Perl can be used to check if a process is running. For example, if I use $$ or $PID to get the process ID for a script and save it to the database.

I'd fire up a script later and see that the current row is being handled by process XXXX but I'd like to know if this process is still running. Is it possible with Perl to take the stored PID and see if it is still running?

scragar
01-13-2009, 02:15 PM
Since you know about PIDs I'm going to guess you are running on a *nix enviroment, right?

In which case can't you do:
$running = `ps -a | grep {$PID}`

halldorr
01-14-2009, 10:26 AM
Yeah I tried this but $running always comes up empty.


$check = $$;
print "This script is $$\n";

$running = `ps -a | grep {$check}`;

print "Running: $running\n\n";


$$ is grabbing the process ID properly.

halldorr
01-14-2009, 10:38 AM
Using this:


ps auwx


Pulls information.


root 7770 0.0 0.1 6648 2672 pts/4 S+ 10:37 0:00 /usr/bin/perl ./pidtest.cgi
root 7771 0.0 0.0 4476 964 pts/4 S+ 10:37 0:00 sh -c ps auwx | grep 7770
root 7773 0.0 0.0 3912 672 pts/4 S+ 10:37 0:00 grep 7770


Running it on a non existant pid returns


root 8062 0.0 0.0 4476 964 pts/4 S+ 10:39 0:00 sh -c ps auwx | grep 1212
root 8064 0.0 0.0 3912 676 pts/4 S+ 10:39 0:00 grep 1212

scragar
01-14-2009, 10:39 AM
Yeah, entirely my fault, `` is evaluated before variables, doesn't make sense to me, but meh.
$check = $$;
print "This script is '$$'\n";
$running = `ps -A`;
if( $running =~ /^{$check}\s/){
print "It's running";
}else{
print "It's not running";
}
print "\n";
lower case A will work if the process is started by the same user on the same thing(hard to explain, but I'll try if need be).

halldorr
01-14-2009, 11:32 AM
Excellent, thanks !