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?
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.
/proc/<PID> should exists if the process PID is still running. Use stat() system call to check for file existence.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With