Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a Unix process is running in Perl?

Tags:

perl

In UNIX, I am checking to see if a process is up by executing the following command;

E.g.

psg dtllst pe99

This returns the following output if the process is running;

UID    PID    PPID    C    STIME    TTY    TIME CMD
pe99   1234   1       0 03:29:44    pts/8  0:01 dtllst pe99

Now in Perl, I want to be able to find out whether this process is up or not. So far I am doing the following

`my $checkProc = `psg dttlst | grep $myNode 2>&1`;`    #where $myNode is something like pe01 or pe02 or pe65 or pe99 etc...

Now after this I do the following to see if the above Perl command has returned what I am looking for to see if the process is up;

if ($checkProc =~ m/dtllst $myNode | $myNode/) {
    #yes, process is up
} else {
    #no, process is down
}

However this is not working - specifically, regardless of whether the UNIX process is alive or not, my code ALWAYS evaluates the if statement as true. I know this is wrong. I have tried to escape the "$" character in the regex to see if this was the issue and I have also tried removing the Perl variables from within the regex altogether.

What am I missing here? I know my regex is wrong somewhere :(

Thanks

like image 310
sandeep Avatar asked Nov 29 '22 11:11

sandeep


1 Answers

You could use Proc::ProcessTable to avoid having to launch an external command and parse its output. Something like

use Proc::ProcessTable;
...
my $t = Proc::ProcessTable->new;
my $is_running = grep { $_->{cmndline} =~ /^dtllst $myNode/ } @{$t->table};
like image 73
cjm Avatar answered Dec 15 '22 06:12

cjm