I am looking for a reliable cross-platform way to check if a process with a specific pid is running. Two possible solutions came up:

  1. kill -0 $PID — exit status is 0 if it the process exists and 1 if not, however it also returns 1 for pids that require additional privileges to kill.
  2. ps a | grep "^\s*${PID}" and similar which are plain ugly.

Is there a way to have something like #1, but without the owner limitation?

  • Note that knowing that a process isn't running isn't very useful, because by the time you do something with the result the process may be dead, and may even be a different just-started process after the prior process with the same PID died. The only time you can rely on a PID being meaningful is from its parent, until the next time you call one of the wait* functions. Feb 15, 2012 at 6:23

1 Answer 1


Can you write a small C program? The kill(2) system call does return -1 if your UID doesn't have permission to send a signal to a given process, but errno is set to EPERM in that case, as opposed to ESRCH for a non-existent PID. I'm reasonably certain you could make it portable across Solaris, HP-UX, Linux and the *BSDs. You would have to compile it for each platform.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .