Alexander Barton alex@barton.de writes:
Does "pgrep(1)" see the zombie processes as well?
Yes; they're recognized by pgrep(1), ps(1), etc..
However, they're listed with process state 'Z' by ps(1), so if we can portably control ps(1) output we might be able to filter them out. That is assuming this state flag 'Z' is portable across *nix systems.
POSIX under-specifies ps(1) and doesn't define a field-name for the process state for the -o switch, but on XSI conforming systems it's specified to be the second column when the -l switch is used. (What the hell, POSIX; if it's one of the fields you recognize, why don't you give it a name for the -o switch? I don't get it.)
So if we can rely on XSI conformance plus state 'Z' for zombie, then the following shell functions should work:
process_is_alive(){ ! process_is_dead "$1" && ! process_is_undead "$1" }
process_is_dead(){ ! kill -0 "$1" }
process_is_undead(){ ps -p "$1" -l | { read _ # ignore column headers read _ state _ case $state in (Z*) true ;; (*) false; esac } }
This works on Debian (and should work on SunOS according to manpages).
However, BSDs apparently don't conform to POSIX.2008/XSI. Another option would be to rely on the -o switch accepting "state", which seems portable across GNU, BSD, and Sun, but I don't know about A/UX or any other platforms you intend to support. Should that variant be realized, the `process_is_undead' function would instead be:
process_is_undead(){ case $(ps -p "$1" -o state=) in (Z*) true ;; (*) false ;; esac }
Hope that helps.
Taylan