ruby列表儿童痘痘

如何获取ruby脚本开始的所有子进程的pid?

解决方法

您可以通过以下方式获取当前流程:
Process.pid

详见http://whynotwiki.com/Ruby_/_Process_management.

然后,您可以使用特定于操作的命令来获取代码.在基于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.split(/\s+/)
  parts[2] if parts[3] == pid.to_s and parts[2] != pipe.pid.to_s
end.compact

# Show the child processes.
puts child_pids

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...