There are many ways that we can achieve this.
If you are using unix system, I hope the following 2 ways will you
use strict;
use warnings;
my $CMD = '/home/blah/my_process';
my $SLEEP_TIME = 1; # check to see if it is running every 5 seconds
while(1)
{
my @pid_array = `ps -ef | grep /home/blah/my_process`;
my $pid_count = scalar(@pid_array);
if ($pid_count == 1) {
# It isn't running, that 1 is for grep. more than one tells you the pr
+ocess is alive.
system "$CMD"; # run it
}
elsif ($pid_count > 2) {
# There is more than one instance running.
# Do appropriate stuff
print "many instances are running";
}
sleep $SLEEP_TIME;
The above code won't work all the time, but, you can adopt some changes and use the above to find a process is running or not
This is another way. Try the below code
use strict;
use warnings;
if (kill (0, pid)) {
print "process alive";
}
else {
print "process died";
}
To know about kill function, try google it. |