Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers 4

I'm not familiar with psg, but I know (the hard way, so to speak) if I run ps ax | grep progname to see if any process is running that contains progname, I'll always get a yes, since grep progname is itself a command that contains "progname".

I don't know perl, but in bash, I get around this like this, e.g., if I want to check if any instance of progname is running, I run ps ax | grep -c "[p]rogname", which solves the problem of always getting a yes.

share|improve this answer
add comment

The command pgrep prints the list of PIDs for all processes (other than itself) that match a grep-compatible regular expression.

It prints nothing if there are no matches.

This should be a sufficient test: [ $(pgrep process | wc) -gt 0 ] is true if the process is running. False if not.

share|improve this answer
1  
In some cases, it is useful to use flag -f with the pgrep. It enables searching for full command line name. For instance, if a program is started with java -jar bla.jar, pgrep bla would fail, while pgrep -f bla would succeed. –  bbaja42 Jan 4 '12 at 5:34
    
I have absolutely no idea why you are using [. Just use pgrep process > /dev/null 2>&1. –  Chris Down Feb 25 '12 at 14:25
add comment

Your regexp m/dtllst $myNode | $myNode/ will match (in particular) any occurrence of $myNode. By construction, psg dtllst | grep $myNode will return something that matches $myNode, so you regexp always matches.

If you cannot follow other people's advice and use pgrep or ps ... | grep or variants thereof, you could either:

  1. set "$checkProc = psg dtllst | grep 'dtllst $myNode';" and then just test if $checkProc is non-empty, or

  2. set "$checkProc = psg dtllst;" and then match it against m/dtllst $myNode/

  3. Use the Proc::ProcessTable CPAN module and just search for an entry $p such that $p->cmdline =~ m/dtllst $myNode/

share|improve this answer
add comment

I don't know if that's what you're looking for, but this is how you can get the PID of your program:

ps -e -o pid= -o args= | sed 's/^\s*//' | grep 'dtllst pe99' | cut -d' ' -f1

This will output all, if any PIDs for the given command with arguments.

You can use that to pipe it into another program, if you like, and if you need all of the PIDs at once, you can wrap the whole thing in $():

MyPIDs=$(ps -e -o pid= -o args= | sed 's/^\s*//' | grep 'dtllst pe99' | cut -d' ' -f1)
echo $MyPIDs
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.