-->

在Ruby的PID获取进程状态(Get process status by pid in Ruby)

2019-07-29 09:22发布

有没有一种方法基于其在Ruby中PID得到进程的子进程状态?

例如,在Python中,你可以做psutil.Process(PID).STATUS

Answer 1:

我不知道便携式红宝石的方法来获得一个正在运行的进程的进程状态。 你可以做Process.wait和检查$?.exitstatus ,但是,这并不像你想要什么。 对于POSIX的解决方案,你可以使用

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

把信码PS生产过程状态

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.


Answer 2:

我一直在寻找同样的事情。 这是一个耻辱ProcessStatus似乎并不能够得到来自现场的PID初始化。 这是非常重要的东西,如果你想要做像一个子进程的安全定时杀生。

在任何情况下,它在第二行/proc/$pid/status ,如果你是在Linux上: status_line = File.open("/proc/#{pid}/status") {|f| f.gets; f.gets } status_line = File.open("/proc/#{pid}/status") {|f| f.gets; f.gets }

最有可能的多速度远远快于涉及外部程序什么。



Answer 3:

在OS X上,我设置的字符串:

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

然后在%×调用执行:

termoutput=%x[#{outputstring}]

我可以显示如果需要的话,或者只是保持输出干净的,我发现了与调用国家行为。



文章来源: Get process status by pid in Ruby