0

Given a pid, for example 29264, how to get to know whether the process is running ?

Is there any easy way to do that ?

thx

爱国者
  • 4,298
  • 9
  • 47
  • 66
  • 2
    As a note: usually PID is *not enough* to check whether a particular process is *still* running. If a process exits, its PID can be reused by some other process, so it's usually not a safe assumption. If you aren't spawning many processes of the same name, you could also involve checking the process name. But the best way is through a lock file. But it all depends on what you are trying to do. – Michał Górny Jul 18 '12 at 09:16
  • Please see [Process Management](http://mywiki.wooledge.org/ProcessManagement). – Dennis Williamson Jul 18 '12 at 10:18
  • Let me add one point: *unless* the process you watch is a _child process_, there're several pitfalls. See [this comment](http://stackoverflow.com/questions/1058047/wait-for-any-process-to-finish/1127675#comment24774342_1058047) for the details. – teika kazura Jun 15 '13 at 07:45

3 Answers3

3

Process status (ps) provides the information you're looking for:

ps -p 29264

Output in case the process is running (quick example on my Mac, works the same on Linux):

PID     TIME       CMD
127     4:54.03    /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder

Otherwhise:

PID     TIME       CMD
Anne
  • 26,765
  • 9
  • 65
  • 71
3

kill -0 29264 ,and inspect the error (if any)

link to online linux (man 2) manual

link to online linux (man 1) manual

wildplasser
  • 43,142
  • 8
  • 66
  • 109
  • excuse me ,what is the meaning of `kill -0 ` , since `kill -l` does not provide the **zero** signal ? – 爱国者 Jul 18 '12 at 09:17
  • 1
    From The Fine Manual: "Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0." The meaning of `kill -0` is : perform all the necessary steps for sending a signal, but don't deliver any actual signal to the process. But the error reporting is the same as for a normal signal. See also `man 2 kill` – wildplasser Jul 18 '12 at 09:20
  • Could you provide a link for that instruction ? – 爱国者 Jul 18 '12 at 09:24
2

To get process status:

ps -p 29264 > /dev/null; echo $?
igustin
  • 1,110
  • 8
  • 8