如何确定在Linux上使用Java或JRuby是否正在运行其他进程ID? [英] How can I determine if a different process id is running using Java or JRuby on Linux?

查看:82
本文介绍了如何确定在Linux上使用Java或JRuby是否正在运行其他进程ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要查看给定的进程ID是否正在运行,并且它必须可以在Java或JRuby(最好是Ruby解决方案)中运行.对于Linux(特别是Debian和/或Ubuntu),它可能与系统有关.

I need to see if a given process id is running, and it must work in either Java or JRuby (preferably a Ruby solution). It can be system dependent for Linux (specifically Debian and/or Ubuntu).

我已经有了要查找的PID,只需要查看它当前是否正在运行即可.

I already have the PID I am looking for, just need to see if it is currently running.

更新:

感谢大家的所有回应!我很感激,但是并不是我想要的...我希望在标准的Ruby库(或Java,但最好是Ruby)中寻找一些东西...如果不存在这样的库调用,我可能会坚持我已经拥有的procfs解决方案.

Thanks for all the responses everyone! I appreciate it, however it's not QUITE what I'm looking for... I am hoping for something in a standard Ruby library (or Java, but preferably Ruby)... if no such library call exists, I will probably stick with the procfs solution I already have.

推荐答案

Darron's comment was spot on, but rather than calling the "kill" binary, you can just use Ruby's Process.kill method with the 0 signal:

#!/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

[user @ host用户] $ ./is_running.rb 14302
14302正在运行

[user@host user]$ ./is_running.rb 14302
14302 is running

[用户@主机用户] $ ./is_running.rb 99999
99999没有运行.

[user@host user]$ ./is_running.rb 99999
99999 is NOT running.

[user @ host用户] $ ./is_running.rb 37
没有查询37的权限!

[user@host user]$ ./is_running.rb 37
No permission to query 37!

[user @ host用户] $ sudo ./is_running.rb 37
37正在运行

[user@host user]$ sudo ./is_running.rb 37
37 is running

参考: http://pleac.sourceforge.net/pleac_ruby/processmanagementetc.html

这篇关于如何确定在Linux上使用Java或JRuby是否正在运行其他进程ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆