views:

1670

answers:

5

The question title says it all. If there is more than one way, please list them. :) I only know of one, but I'm wondering if there is a cleaner, in-Ruby way.

A: 

Parse the output of ps.

Pistos
Note that parsing the output may help tell you if a process has that PID, but it may not be "your" process.
Dustin
@Dustin: Right. Good point.
Pistos
Any of the above methods will only tell you if a process exist at a certain PID, not if it is indeed your process.Parsing the various columns of PS output would be a good way of determining with high likelihood if the process is yours.
Douglas F Shearer
+12  A: 

If it's a process you expect to "own" (e.g. you're using this to validate a pid for a process you control), you can just send sig 0 to it.

>> Process.kill 0, 370
=> 1
>> Process.kill 0, 2
Errno::ESRCH: No such process
    from (irb):5:in `kill'
    from (irb):5
>>
Dustin
Interesting approach! I'll investigate this.
Pistos
dang, beat me to it.I saw the "new answers have been posted" thing and my heart sank :(
John T
But what's signal 0? A noop?
Pistos
From my man page: A value of 0, however, will cause error checking to be performed (with no signal being sent). This can be used to check the validity of pid.
Dustin
Dang, was looking at signals man page, etc. Didn't think to look at kill's manpage. :) Thanks, Dustin.
Pistos
@John heh, sorry. Same thing happening to me. This place is cut throat.
Dustin
+2  A: 

You can try using

Process::kill 0, pid

where pid is the pid number, if the pid is running it should return 1.

John T
+7  A: 

@John T, @Dustin: Actually, guys, I perused the Process rdocs, and it looks like

Process.getpgid( pid )

is a less violent means of applying the same technique.

Pistos
A: 

The difference between the Process.getpgid and Process::kill approaches seems to be what happens when the pid exists but is owned by another user. Process.getpgid will return an answer, Process::kill will throw an exception (Errno::EPERM).

Based on that, I recommend Process.getpgid, if just for the reason that it saves you from having to catch two different exceptions.

Here's the code I use:

begin
  Process.getpgid( pid )
  true
rescue Errno::ESRCH
  false
end
tonystubblebine