Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portable shell solution to check if PID is zombied

Tags:

I want to check if a PID is running (that is, exists and is not zombied).

It's really quick to do from /proc/$PID/stat but I'd like something more portable.

The best I have right now is:

( STAT="$(ps -ostat= -p$PID)"; test "$STAT" -a "$STAT" "!=" "Z" ) 

Which seems to work on BSD and Linux. Is there a better way?

like image 765
Jason Avatar asked Sep 19 '13 19:09

Jason


People also ask

How do I know if PID is running?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.

How do I know if my PID is still running C?

/proc/<PID> should exists if the process PID is still running. Use stat() system call to check for file existence.


1 Answers

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/sh pid="$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 
like image 183
hairy yuppie Avatar answered Oct 17 '22 04:10

hairy yuppie