我需要查看给定的进程 ID 是否正在运行,并且它必须在 Java 或 JRuby 中工作(最好是 Ruby 解决方案)。它可能取决于 Linux 系统(特别是 Debian 和/或 Ubuntu)。
我已经有了我要找的PID,只需要看看它当前是否正在运行。
UPDATE:
感谢大家的回复!我很欣赏它,但这并不是我正在寻找的东西...我希望在标准 Ruby 库(或 Java,但最好是 Ruby)中找到一些东西...如果不存在这样的库调用,我可能会坚持使用我已经有了 procfs 解决方案。
达伦的评论 https://stackoverflow.com/questions/141162/how-can-i-determine-if-a-different-process-id-is-running-using-java-or-jruby-on#141731是正确的,但您可以只使用 Ruby 的 Process.kill 方法和 0 信号,而不是调用“kill”二进制文件:
#!/usr/bin/ruby
pid = ARGV[0].to_i
begin
Process.kill(0, pid)
puts "#{pid} is running"
rescue Errno::EPERM # changed uid
puts "No permission to query #{pid}!";
rescue Errno::ESRCH
puts "#{pid} is NOT running."; # or zombied
rescue
puts "Unable to determine status for #{pid} : #{$!}"
end
[用户@主机用户]$ ./is_running.rb 14302
14302 正在运行
[用户@主机用户]$ ./is_running.rb 99999
99999 未运行。
[用户@主机用户]$ ./is_running.rb 37
没有权限查询37!
[用户@主机用户]$ sudo ./is_running.rb 37
37 正在运行
参考:http://pleac.sourceforge.net/pleac_ruby/processmanagementetc.html http://pleac.sourceforge.net/pleac_ruby/processmanagementetc.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)