Portable shell solution to check if PID is zombied
Hopefully POSIX compliant. Tested with dash. To use it, save it with your favorite editor, make it executable (chmod 755 foo.sh
), and run it with a PID argument.
Of course you can adapt it as needed.
#!/bin/shpid="$1";psout=$(ps -o s= -p "$pid");pattern='[SRDTWX]';case "$psout" in $pattern) echo "Not a zombie";; Z) echo "Zombie found";; *) echo "Incorrect input";; esac
IMHO parsing the output of 'ps' is the most portable way. all the 'ps' variant out there differ a little bit in the syntax, but the overall output is good enough:
#!/bin/shprocess_show(){ ps ps ax}pid_is_zombie(){ pid="$1" process_show | while read -r LINE; do # e.g.: 31446 pts/7 R+ 0:00 ps ax set -f set +f -- $LINE test "$1" = "$pid" || continue case "$3" in *'Z'*) return 0;; esac done return 1}pid_is_zombie 123 && echo "yes it is"
even 'ps ax' is not possible everywhere, so we must try 'ps' and 'ps ax'.