Get process status by pid in Ruby

process-management, ruby

Solution

I don't know of a portable ruby method to get process state of a running process. You can do `Process.wait` and check `$?.exitstatus`, but that doesn't look like what you want. For a posix solution, you could use

`ps -o state -p #{pid}`.chomp

to get the letter code ps produces for process state

PROCESS STATE CODES
Here are the different values that the s, stat and state output specifiers
(header "STAT" or "S") will display to describe the state of a process.
D    Uninterruptible sleep (usually IO)
R    Running or runnable (on run queue)
S    Interruptible sleep (waiting for an event to complete)
T    Stopped, either by a job control signal or because it is being traced.
W    paging (not valid since the 2.6.xx kernel)
X    dead (should never be seen)
Z    Defunct ("zombie") process, terminated but not reaped by its parent.

Problem

Is there a way to get a process's child process status based on its PID in Ruby? For example, in Python you can do psutil.Process(pid).status

Original source