How can I check Ruby if a process works with a specific pid?

If there are several ways, list them. I know only one, but I wonder if there is a cleaner, in-ruby way.

+41
ruby process pid
Nov 28 '08 at 5:11
source share
10 answers

If this is a process that you expect "on your own" (for example, you use this to check the pid for a controlled process), you can simply 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 >> 
+44
Nov 28 '08 at 5:17
source share
— -

The difference between the Process.getpgid and Process::kill approaches seems to be what happens when the pid exists but belongs to a different user. Process.getpgid will return a response, Process::kill will throw an exception (Errno::EPERM) .

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

Here is the code I'm using:

 begin Process.getpgid( pid ) true rescue Errno::ESRCH false end 
+54
Aug 25 '10 at 16:43
source share

@John T, @Dustin: Actually guys, I looked at Process rdocs and looks like

 Process.getpgid( pid ) 

is a less violent means of applying the same technique.

+45
Nov 28 '08 at 5:23
source share

For child processes, other solutions, such as sending a signal, will not behave as expected: they will indicate that the process is still running when it really exited.

You can use Process.waitpid if you want to check the process that you spawned on your own. The call will not be blocked if you use the Process::WNOHANG , and nil will be returned until the child process terminates.

Example:

 pid = Process.spawn('sleep 5') Process.waitpid(pid, Process::WNOHANG) # => nil sleep 5 Process.waitpid(pid, Process::WNOHANG) # => pid 

If pid does not belong to the child process, an exception will be thrown ( Errno::ECHILD: No child processes ).

The same goes for Process.waitpid2 .

+24
Jan 17 '13 at 15:00
source share

You can try using

 Process::kill 0, pid 

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

+4
Nov 28 '08 at 5:19
source share

On Linux, you can get many attributes of a running program using the proc file system:

 File.read("/proc/#{pid}/cmdline") File.read("/proc/#{pid}/comm") 
+4
Jan 03 '14 at 11:35
source share

Here is how I did it:

 def alive?(pid) !!Process.kill(0, pid) rescue false end 
+4
Sep 10 '15 at 23:44
source share

A *nix - the only approach is to execute a shell-out before ps and check if the \n separator (new line) exists in the returned string.

IRB Output Example

 1.9.3p448 :067 > `ps -p 56718` " PID TTY TIME CMD\n56718 ttys007 0:03.38 zeus slave: default_bundle \n" 

Packed as a method

 def process?(pid) !!`ps -p #{pid.to_i}`["\n"] end 
0
Sep 05 '13 at 14:40
source share

I have addressed this problem before, and yesterday I compiled it into " process_exists .

It sends a zero signal (0) to the process with the given pid to check if it exists. It works even if the current user does not have the right to send a signal to the receiving process.

Using:

 require 'process_exists' pid = 12 pid_exists = Process.exists?(pid) 
0
Aug 05 '14 at 18:11
source share

This worked for me when I checked if a song played in Ruby code

 def process_running?(pid) return true if pid false end def play_song_for_real @process_id = fork { exec 'afplay', $song_path } end 

if you check process_running(@process_id) you will get the truth if the process is running or false if it is not

-3
Aug 31 '16 at 2:59
source share



All Articles