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.
如果有不止一种方法,请列出它们。我只知道其中一个,但我想知道是否有一种更干净的ruby方式。
9 个解决方案
#1
45
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.
如果它是一个您希望“拥有”的进程(例如,您正在使用它来验证您控制的进程的pid),那么您可以将sig 0发送给它。
>> Process.kill 0, 370
=> 1
>> Process.kill 0, 2
Errno::ESRCH: No such process
from (irb):5:in `kill'
from (irb):5
>>
#2
55
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)
.
过程之间的区别。getpgid和Process:::kill方法似乎是当pid存在时发生的事情,但是它属于另一个用户。的过程。getpgid将返回一个答案,Process: kill将抛出一个异常(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
#3
45
@John T, @Dustin: Actually, guys, I perused the Process rdocs, and it looks like
@John T, @Dustin:事实上,伙计们,我仔细研究了rdocs的流程,看起来是这样的
Process.getpgid( pid )
is a less violent means of applying the same technique.
是一种不那么暴力的方法。
#4
24
For child processes, other solutions like sending a signal won't behave as expected: they will indicate that the process is still running when it actually exited.
对于子进程,像发送信号这样的其他解决方案不会像预期的那样运行:它们将表明进程实际上退出时仍在运行。
You can use Process.waitpid if you want to check on a process that you spawned yourself. The call won't block if you're using the Process::WNOHANG
flag and nil
is going to be returned as long as the child process didn't exit.
您可以使用过程。如果您想检查自己生成的进程,请使用waitpid。如果您正在使用Process::WNOHANG标志,并且只要子进程没有退出,就会返回nil。
Example:
例子:
pid = Process.spawn('sleep 5')
Process.waitpid(pid, Process::WNOHANG) # => nil
sleep 5
Process.waitpid(pid, Process::WNOHANG) # => pid
If the pid doesn't belong to a child process, an exception will be thrown (Errno::ECHILD: No child processes
).
如果pid不属于子进程,将抛出异常(Errno::ECHILD: No子进程)。
The same applies to Process.waitpid2.
这同样适用于Process.waitpid2。
#5
4
You can try using
你可以尝试使用
Process::kill 0, pid
where pid is the pid number, if the pid is running it should return 1.
在pid为pid的地方,如果pid在运行,它应该返回1。
#6
4
Under Linux you can obtain a lot of attributes of running programm using proc filesystem:
在Linux下,您可以使用proc文件系统获得运行程序的许多属性:
File.read("/proc/#{pid}/cmdline")
File.read("/proc/#{pid}/comm")
#7
4
This is how I've been doing it:
我就是这样做的:
def alive?(pid)
!!Process.kill(0, pid) rescue false
end
#8
0
A *nix
-only approach would be to shell-out to ps
and check if a \n
(new line) delimiter exists in the returned string.
只有*nix的方法是扩展到ps并检查返回的字符串中是否存在一个\n(新行)分隔符。
Example IRB Output
IRB输出示例
1.9.3p448 :067 > `ps -p 56718`
" PID TTY TIME CMD\n56718 ttys007 0:03.38 zeus slave: default_bundle \n"
Packaged as a Method
打包为一个方法
def process?(pid)
!!`ps -p #{pid.to_i}`["\n"]
end
#9
0
I've dealt with this problem before and yesterday I compiled it into the "process_exists" gem.
我以前处理过这个问题,昨天我将它编译为“process_exist”gem。
It sends the null signal (0) to the process with the given pid to check if it exists. It works even if the current user does not have permissions to send the signal to the receiving process.
它用给定的pid将空信号(0)发送到进程,以检查它是否存在。即使当前用户没有将信号发送到接收进程的权限,它仍然有效。
Usage:
用法:
require 'process_exists'
pid = 12
pid_exists = Process.exists?(pid)
#1
45
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.
如果它是一个您希望“拥有”的进程(例如,您正在使用它来验证您控制的进程的pid),那么您可以将sig 0发送给它。
>> Process.kill 0, 370
=> 1
>> Process.kill 0, 2
Errno::ESRCH: No such process
from (irb):5:in `kill'
from (irb):5
>>
#2
55
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)
.
过程之间的区别。getpgid和Process:::kill方法似乎是当pid存在时发生的事情,但是它属于另一个用户。的过程。getpgid将返回一个答案,Process: kill将抛出一个异常(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
#3
45
@John T, @Dustin: Actually, guys, I perused the Process rdocs, and it looks like
@John T, @Dustin:事实上,伙计们,我仔细研究了rdocs的流程,看起来是这样的
Process.getpgid( pid )
is a less violent means of applying the same technique.
是一种不那么暴力的方法。
#4
24
For child processes, other solutions like sending a signal won't behave as expected: they will indicate that the process is still running when it actually exited.
对于子进程,像发送信号这样的其他解决方案不会像预期的那样运行:它们将表明进程实际上退出时仍在运行。
You can use Process.waitpid if you want to check on a process that you spawned yourself. The call won't block if you're using the Process::WNOHANG
flag and nil
is going to be returned as long as the child process didn't exit.
您可以使用过程。如果您想检查自己生成的进程,请使用waitpid。如果您正在使用Process::WNOHANG标志,并且只要子进程没有退出,就会返回nil。
Example:
例子:
pid = Process.spawn('sleep 5')
Process.waitpid(pid, Process::WNOHANG) # => nil
sleep 5
Process.waitpid(pid, Process::WNOHANG) # => pid
If the pid doesn't belong to a child process, an exception will be thrown (Errno::ECHILD: No child processes
).
如果pid不属于子进程,将抛出异常(Errno::ECHILD: No子进程)。
The same applies to Process.waitpid2.
这同样适用于Process.waitpid2。
#5
4
You can try using
你可以尝试使用
Process::kill 0, pid
where pid is the pid number, if the pid is running it should return 1.
在pid为pid的地方,如果pid在运行,它应该返回1。
#6
4
Under Linux you can obtain a lot of attributes of running programm using proc filesystem:
在Linux下,您可以使用proc文件系统获得运行程序的许多属性:
File.read("/proc/#{pid}/cmdline")
File.read("/proc/#{pid}/comm")
#7
4
This is how I've been doing it:
我就是这样做的:
def alive?(pid)
!!Process.kill(0, pid) rescue false
end
#8
0
A *nix
-only approach would be to shell-out to ps
and check if a \n
(new line) delimiter exists in the returned string.
只有*nix的方法是扩展到ps并检查返回的字符串中是否存在一个\n(新行)分隔符。
Example IRB Output
IRB输出示例
1.9.3p448 :067 > `ps -p 56718`
" PID TTY TIME CMD\n56718 ttys007 0:03.38 zeus slave: default_bundle \n"
Packaged as a Method
打包为一个方法
def process?(pid)
!!`ps -p #{pid.to_i}`["\n"]
end
#9
0
I've dealt with this problem before and yesterday I compiled it into the "process_exists" gem.
我以前处理过这个问题,昨天我将它编译为“process_exist”gem。
It sends the null signal (0) to the process with the given pid to check if it exists. It works even if the current user does not have permissions to send the signal to the receiving process.
它用给定的pid将空信号(0)发送到进程,以检查它是否存在。即使当前用户没有将信号发送到接收进程的权限,它仍然有效。
Usage:
用法:
require 'process_exists'
pid = 12
pid_exists = Process.exists?(pid)