Get pid process status in Ruby

Is there a way to get the status of a child process of a process based on its PID in Ruby?

For example, in Python you can do psutil.Process (pid) .status

+5
source share
3 answers

I do not know the portable ruby โ€‹โ€‹method to get the state of the process of a running process. You can do Process.waitand check $?.exitstatus, but that doesn't look like what you want. For posix solution you can use

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

to get the letter code ps for process status

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.
+2
source

I was looking for the same thing. This is a shame. ProcessStatus doesn't seem to be able to initialize from live pid. This is vital material if you want to do anything as a safe time to kill a child process.

, /proc/$pid/status, Linux.: status_line = File.open("/proc/#{pid}/status") {|f| f.gets; f.gets }

, , -, .

+2

OS X :

outputstring="ps -O=S -p #{mypid}"

% x:

termoutput=%x[#{outputstring}]

I can display this if necessary or simply keep the output clean and act in the state that I found when called.

0
source

All Articles