如何确定 Linux 上是否使用 Java 或 JRuby 运行不同的进程 ID?

发布于 2024-07-06 23:21:28 字数 289 浏览 4 评论 0原文

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

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


更新:

感谢大家的回复! 我很欣赏它,但这并不是我正在寻找的东西...我希望在标准 Ruby 库(或 Java,但最好是 Ruby)中找到一些东西...如果不存在这样的库调用,我可能会坚持使用我已经有了 procfs 解决方案。

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).

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


UPDATE:

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.

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

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

发布评论

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

评论(7

北恋 2024-07-13 23:21:28

Darron 的评论 很准确,但是您可以使用带有 0 信号的 Ruby 的 Process.kill 方法,而不是调用“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

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 user]$ ./is_running.rb 14302
14302 is running

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

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

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

Reference: http://pleac.sourceforge.net/pleac_ruby/processmanagementetc.html

爱人如己 2024-07-13 23:21:28

Unix 有一个特殊功能,即围绕信号 0 进行终止系统调用。 执行错误检查,但不发送信号。

def pid_exists? (pid)
    system "kill -0 #{pid}"
    return $? == 0
end

需要注意的是:这不会检测到您无权发出信号的 pid 进程。

Unix has a special feature of the kill system call around signal zero. Error checking is performed, but no signal is sent.

def pid_exists? (pid)
    system "kill -0 #{pid}"
    return $? == 0
end

One caveat: this won't detect processes with that pid that you don't have permission to signal.

少女七分熟 2024-07-13 23:21:28

来自我对这个问题的回答,我想再次使用 procfs,通过 File.exist 检查给定目录是否存在? “/proc/#{pid}”。 这在 jirb 中有效:

irb(main):001:0> File.exist? "/proc/5555"
=> false
irb(main):002:0> File.exist? "/proc/7677"
=> true

但是,我仍然更喜欢使用专门存在的方法来检测进程是否正在运行...比如 Process.exist?(pid)...不幸的是我见过的不存在。

From my answer to this question, I was thinking of just using procfs again, by checking if the given directory exists via File.exist? "/proc/#{pid}". This worked in jirb:

irb(main):001:0> File.exist? "/proc/5555"
=> false
irb(main):002:0> File.exist? "/proc/7677"
=> true

However, I would still prefer to use a method that specifically exists to detect if a process is running... like Process.exist?(pid)... which unfortunately doesn't exist that I've seen.

独留℉清风醉 2024-07-13 23:21:28

我不能代表 JRuby,但在 Java 中,唯一的检查方法是您是否从 Java 启动了该进程(在这种情况下,您将拥有一个 进程,你可以用它来做事)。

I can't speak for JRuby, but in Java, the only way to check is if you launched the process from Java (in which case you would have an instance of Process that you could do things with).

梦途 2024-07-13 23:21:28

您可能需要仔细检查您正在使用的 JVM。 但如果你发送一个 SIGQUIT 信号kill -3 我相信,(我手头没有终端)。 这应该生成一个 Javacore 文件,其中包含正在使用的线程的堆栈跟踪,检查该文件中的 JRuby 包。

它不应该终止或发生任何事情,但一如既往地小心发送信号。

You'll probably want to double check for the JVM that you're using. But if you send a SIGQUIT signal kill -3 I believe, (I don't have a terminal handy). That should generate a Javacore file which will have stack traces of the in use thread, check for JRuby packages in that file.

It shouldn't terminate or anything but as always be careful sending signals.

孤千羽 2024-07-13 23:21:28

如果您不介意创建一个全新的流程,那么这种懒惰的方式应该可行:

def pid_exists? (pid)
    system "ps -p #{pid} > /dev/null"
    return $? == 0
end

对于 ps 的大多数变体,它应该在成功时返回 0,在错误时返回非零。 上述用法的常见错误是找不到具有给定 PID 的进程。 在这种情况下,我在 Ubuntu 下的 ps 版本返回 256。

您还可以使用 Process.kill 向进程发送 0 信号(信号 0 表示是否可以发送信号),但这似乎只有在您拥有要向其发送信号的进程时才有效(或者以其他方式向其发送信号的权限)。

If you don't mind creating a whole new process then this lazy way should work:

def pid_exists? (pid)
    system "ps -p #{pid} > /dev/null"
    return $? == 0
end

For most variations of ps, it should return 0 on success and non-zero on error. The usual error with the usage above will be not finding the process with the given PID. The version of ps I have under Ubuntu returns 256 in this case.

You could also use Process.kill to send a signal of 0 to the process (signal 0 indicates if a signal may be sent), but that seems to only work if you own the process you're sending the signal to (or otherwise have permissions to send it signals).

我纯我任性 2024-07-13 23:21:28

您可以使用 Java 安装附带的命令行工具 jps。 jps 列出用户的所有 java 进程。

例如,

>jps -l
5960 org.jruby.Main
2124 org.jruby.Main
5376 org.jruby.Main
4428 sun.tools.jps.Jps

或者如果您需要将结果放入脚本中,您可以使用 %x[..]:

>> result = %x[jps -l]
=> "5960 org.jruby.Main\n2264 sun.tools.jps.Jps\n2124 org.jruby.Main\n5376 org.jruby.Main\n"
>> p result
"5960 org.jruby.Main\n2264 sun.tools.jps.Jps\n2124 org.jruby.Main\n5376 org.jruby.Main\n"
=> nil

You could use the command line tool jps which comes with your java installation. jps lists all java Processes of a user.

E.g.

>jps -l
5960 org.jruby.Main
2124 org.jruby.Main
5376 org.jruby.Main
4428 sun.tools.jps.Jps

Or if you need to get the results into your script you could use %x[..]:

>> result = %x[jps -l]
=> "5960 org.jruby.Main\n2264 sun.tools.jps.Jps\n2124 org.jruby.Main\n5376 org.jruby.Main\n"
>> p result
"5960 org.jruby.Main\n2264 sun.tools.jps.Jps\n2124 org.jruby.Main\n5376 org.jruby.Main\n"
=> nil
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文