4

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...

  • You don't need that "for" loop and multiple ps(1) invocations, FWIW: ps $(pgrep ...). – pilcrow May 23 '11 at 16:56
6

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).

| 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
0

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
| 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
0
## bash

## function to check if a process is alive and running:

_isRunning() {
    ps -o comm= -C "$1" 2>/dev/null | grep -x "$1" >/dev/null 2>&1
}

## example 1: checking if "gedit" is running

if _isRunning gedit; then
    echo "gedit is running"
else
    echo "gedit is not running"
fi

## example 2: start lxpanel if it is not there

if ! _isRunning lxpanel; then
    lxpanel &
fi

## or

_isRunning lxpanel || (lxpanel &)

Note: pgrep -x lxpanel or pidof lxpanel still reports that lxpanel is running even when it is defunct (zombie); so to get alive-and-running process, we need to use ps and grep

| improve this answer | |
0

List processes whose command lines match a string:

livepgrep(){ ps o state=,pid=,command=|sed -E -n "/ sed -E -n/d;/^[^ZT] +[0-9]+ .*$@/p"; }
              ^ ^   ^      ^      ^      ^  ^         ^            ^      ^        ^
              | |   |      |      |      |  |         |            |      |        |
processes ----+ |   |      |      |      |  |         |            |      |        |
output format --+   |      |      |      |  |         |            |      |        |
process state ------+      |      |      |  |         |            |      |        |
pid -----------------------+      |      |  |         |            |      |        |
full command line ----------------+      |  |         |            |      |        |
sed as the filter -----------------------+  |         |            |      |        |
use extended regex -------------------------+         |            |      |        |
exclude sed from search results ----------------------+            |      |        |
state != Z (zombie, defunct) != T (stopped) -----------------------+      |        |
pid ----------------------------------------------------------------------+        |
string to search in the command line ----------------------------------------------+

Examples:

$ livepgrep tint2
S   493 tint2
$ livepgrep python
S   525 /usr/bin/python /bin/udiskie -C -F -s
S   580 python2 /home/xxx/bin/twmcpuram
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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