tags:

views:

118

answers:

3

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

+1  A: 

Adding to @zigdon's answer:

Lets say your $myNode is foo, your regex will be /dtllst foo | foo/

Now this searches for the string 'dtllst foo ' or ' foo' in $checkProc.

Notice that there is a space after 'foo' in 'dtllst foo '. The only place this string could be found is in the last column as CMD but the trailing space will cause the match to fail.

Also your alternative ' foo' has a space too. If the only way to find the process is to search for 'dtllst foo' there is no need for this alternative as this alternative will also match 'foo' running as an argument to some other command.

And the regex for which is :

if ($checkProc =~ m/dtllst $myNode/) {
codaddict
+2  A: 

Are you perhaps matching the grep process? You could always add a | grep -v grep to make sure you're filtering that line out of the ps output.

zigdon
+6  A: 

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};
cjm