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.

Is there a combination of command-line options for ps or pgrep or some other relatively direct way to determine if a particular process name is actually running (available for normal use)..

By "running", I mean to specifically exclude processes which are <defunct> or any other non-running processes (eg. zombies :)...

This sample script shows an example of <defunct> items:

#!/bin/bash   ubuntu 10.04

  pgrep ^gnuserv$
# 25591
# 25599
# 27330

  ps $(pgrep ^gnuserv$)  # command ammended as per pilcrow's good suggestion
#   PID TTY      STAT   TIME COMMAND
# 25591 ?        Zs     0:00 [gnuserv] <defunct>
# 25599 ?        Zs     0:00 [gnuserv] <defunct>
# 27330 pts/2    S+     0:00 gnuserv

I could further sed the output, but I think/hope there's a more direct way...

share|improve this question
    
You don't need that "for" loop and multiple ps(1) invocations, FWIW: ps $(pgrep ...). –  pilcrow May 23 '11 at 16:56
add comment

2 Answers

up vote 5 down vote accepted

In your comment you clarify:

I'm actually looking for a single step option to ps or pgrep (or similar) which only outputs "active" processes...

I'm afraid you're out of luck with current ps/pgrep implementations.

Post filtering like this relies on a full understanding of the intial output, which I don't have...

But you can get that understanding and, better yet, control that output as desired. Try something like this:

$ function pgrep_live {
>   pids=$(pgrep "$1");
>   [ "$pids" ] || return;
>   ps -o s= -o pid= $pids | sed -n 's/^[^ZT][[:space:]]\+//p';
> }

That will return the pids for any pgrep'd processes matching your input string, which processes are "available for normal use," that is, neither dead+unreaped (Z) nor stopped (T).

share|improve this answer
    
This looks like what I'm after... The STAT column seems like it holds the information I need (ie, the status), so although there isn't a direct option, it would be quite reliable to test for a specific status via sed.... I'll have a closer look at this a bit later...it is looking good –  Peter.O May 23 '11 at 21:18
    
Thanks.. your answer has cut through the fog... The once-was gobbledegook in man ps is now becoming meaningful.. I see why you said "...and, better yet, control that output as desired." ... it is very customizable, and the stat codes legend is under PROCESS STATE CODES in the man page.. –  Peter.O May 24 '11 at 8:47
add comment

you can try with the -v option of grep which negate the regex like this :

for p in $(pgrep ^gnuserv$) ;do ps x |grep "^$p" | grep -v \<defunct\> ;done
share|improve this answer
    
sed and grep are pretty much the same in this situation... I'm actually looking for a single step option to ps or pgrep (or similar) which only outputs "active" processes... Post filtering like this relies on a full understanding of the intial output, which I don't have... eg. [gnuserv] <defunct> took me conpletely by surprise (what other surprises can I expect?.. –  Peter.O May 23 '11 at 13:41
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.