ruby 列表子 pid

发布于 2024-08-15 13:42:36 字数 36 浏览 5 评论 0原文

如何获取从 ruby​​ 脚本启动的所有子进程的 pid?

How can I get pids of all child processes which were started from ruby script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音盲 2024-08-22 13:42:36

您可以通过以下方式获取当前进程:

Process.pid

请参阅 http://whynotwiki.com/Ruby_/_Process_management 了解更多信息细节。

然后就可以使用操作特定的命令来获取子进程的pid。在基于 UNIX 的系统上,这将类似于

# Creating 3 child processes.
IO.popen('uname')
IO.popen('uname')
IO.popen('uname')

# Grabbing the pid.
pid = Process.pid

# Get the child pids.
pipe = IO.popen("ps -ef | grep #{pid}")

child_pids = pipe.readlines.map do |line|
  parts = line.lstrip.split(/\s+/)
  parts[1] if parts[2] == pid.to_s and parts[1] != pipe.pid.to_s
end.compact

# Show the child processes.
puts child_pids

在 osx+ubuntu 上测试。

我承认这可能不适用于所有 unix 系统,因为我相信 ps -ef 的输出在不同的 unix 风格上略有不同。

You can get the current process with:

Process.pid

see http://whynotwiki.com/Ruby_/_Process_management for further details.

Then you could use operating specific commands to get the child pids. On unix based systems this would be something along the lines of

# Creating 3 child processes.
IO.popen('uname')
IO.popen('uname')
IO.popen('uname')

# Grabbing the pid.
pid = Process.pid

# Get the child pids.
pipe = IO.popen("ps -ef | grep #{pid}")

child_pids = pipe.readlines.map do |line|
  parts = line.lstrip.split(/\s+/)
  parts[1] if parts[2] == pid.to_s and parts[1] != pipe.pid.to_s
end.compact

# Show the child processes.
puts child_pids

Tested on osx+ubuntu.

I admit that this probably doesn't work on all unix systems as I believe the output of ps -ef varies slightly on different unix flavors.

活泼老夫 2024-08-22 13:42:36

Process.fork 用生成的子进程的 PID 进行响应。当你生成孩子时,只需在数组中跟踪它们即可。请参阅http://ruby-doc.org/core/classes/Process.html# M003148

Process.fork responds with the PID of the child spawned. Just keep track of them in an array as you spawn children. See http://ruby-doc.org/core/classes/Process.html#M003148.

微暖i 2024-08-22 13:42:36

也可以使用 sys-proctable gem 来完成:

require 'sys/proctable'

Sys::ProcTable.ps.select{ |pe| pe.ppid == $ }

Can be also done using sys-proctable gem:

require 'sys/proctable'

Sys::ProcTable.ps.select{ |pe| pe.ppid == $ }
慢慢从新开始 2024-08-22 13:42:36

这实际上非常复杂并且是特定于平台的。实际上,如果所有子进程故意隐藏,您无法找到它们。

如果您只想终止生成的进程,有很多选择。对于测试框架我选择了两个:
1. 使用 pgid => 生成进程正确
2.插入变量MY_CUSTOM_COOKIE=asjdkahf,然后在环境中找到带有该cookie的procs并杀死它。

仅供参考,使用 ps 找出进程层次结构是非常不可靠的。如果链中的一个进程退出,则其子进程的父进程 pid 为 1(至少在 Linux 上)。所以不值得实施。

This is actually quiet complicated and is platform specific. You actually cannot find all sub-processes if they deliberately try to hide.

If you want to just kill spawned processes there are many options. For a test framework I chose two:
1. spawn processes with pgid => true
2. insert variable MY_CUSTOM_COOKIE=asjdkahf, then find procs with that cookie in environment and kill it.

FYI using ps to find out process hierarchy is very unreliable. If one process in the chain exits, then its sub-processes get a parent pid of 1 (on linux at least). So it's not worth implementing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文