且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Linux上使用Java或JRuby找到我的PID?

更新时间:2023-09-15 13:51:46

如果你有 procfs ,你可以通过/ proc / self symlink找到进程id,它指向一个名为pid的目录(此处还有文件包含其他相关信息,包括PID,但是在这种情况下你只需要目录。)

If you have procfs installed, you can find the process id via the /proc/self symlink, which points to a directory whose name is the pid (there are also files here with other pertinent information, including the PID, but the directory is all you need in this case).

因此,使用Java,你可以这样做:

Thus, with Java, you can do:

String pid = new File("/proc/self").getCanonicalFile().getName();

在JRuby中,您可以使用相同的解决方案:

In JRuby, you can use the same solution:

pid = java.io.File.new("/proc/self").canonical_file.name

特别感谢免费节点上的#***频道帮助我解决这个问题! (具体来说, Jerub gregh Topdeck

Special thanks to the #*** channel on free node for helping me solve this! (specifically, Jerub, gregh, and Topdeck)