Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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. – Gilles Feb 15 '12 at 6:23
up vote 4 down vote accepted

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.

share|improve this answer
Thanks! That did the job. – Mischa Arefiev Feb 14 '12 at 16:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.