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

发布于 2024-07-06 21:58:12 字数 130 浏览 4 评论 0原文

我需要在Linux平台上找到当前正在运行的进程的PID(它可以是系统相关的解决方案)。 Java 不支持获取进程 ID,而 JRuby 目前存在 Ruby 方法 Process.pid 的错误。

还有其他方法获取PID吗?

I need to find the PID of the current running process on a Linux platform (it can be a system dependent solution). Java does not support getting the process ID, and JRuby currently has a bug with the Ruby method, Process.pid.

Is there another way to obtain the PID?

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

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

发布评论

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

评论(6

穿越时光隧道 2024-07-13 21:58:12

如果您安装了 procfs,您可以通过 /proc/self 符号链接找到进程 ID,它指向一个名为 pid 的目录(这里还有包含其他相关信息的文件,包括 PID,但在本例中您只需要该目录)。

因此,使用 Java,您可以执行以下操作:

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

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

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

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

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

Thus, with Java, you can do:

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

In JRuby, you can use the same solution:

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

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

恰似旧人归 2024-07-13 21:58:12

仅在使用 Sun JVM 的 Linux 中进行了测试。 可能不适用于其他 JMX 实现。

String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];

Only tested in Linux using Sun JVM. Might not work with other JMX implementations.

String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
早茶月光 2024-07-13 21:58:12

可以使用JNI接口调用POSIX函数 getpid()。 这非常简单。 您从所需的 POSIX 函数的类开始。 我将其称为 POSIX.java

import java.util.*;

class POSIX
{
    static { System.loadLibrary ("POSIX"); }
    native static int getpid ();
}

编译它

$ javac POSIX.java

之后,生成一个头文件 POSIX.h

$ javah -jni POSIX

该头文件包含该函数的 C 原型,并包装了 getpid 函数。 现在您必须实现该功能,这非常简单。 我是在 POSIX.c 中完成的:

#include "POSIX.h"

#include <sys/types.h>
#include <unistd.h>

JNIEXPORT jint JNICALL Java_POSIX_getpid (JNIEnv *env, jclass cls)
{
    return getpid ();
}

现在您可以使用 gcc 来编译它:

$ gcc -Wall -I/usr/lib/jvm/java-1.6.0-sun-1.6.0.21/include -I/usr/lib/jvm/java-1.6.0-sun-1.6.0.21/include/linux -o libPOSIX.so -shared -Wl,-soname,libPOSIX.so POSIX.c -static -lc

您必须指定 Java 的安装位置。 就这样。 现在您可以使用它了。 创建一个简单的 getpid 程序:

public class getpid
{
    public static void main (String argv[])
    {
        System.out.println (POSIX.getpid ());
    }
}

使用 javac getpid.java 编译它并运行它:

$ java getpid &
[1] 21983
$ 21983

第一个 pid 由 shell 写入,第二个 pid 由 shell 提示符返回后的 Java 程序写入。 ∎

You can use the JNI interface to call the POSIX function getpid(). It is quite straight forward. You start with a class for the POSIX functions you need. I call it POSIX.java:

import java.util.*;

class POSIX
{
    static { System.loadLibrary ("POSIX"); }
    native static int getpid ();
}

Compile it with

$ javac POSIX.java

After that you generate a header file POSIX.h with

$ javah -jni POSIX

The header file contains the C prototype for the function with wraps the getpid function. Now you have to implement the function, which is quite easy. I did it in POSIX.c:

#include "POSIX.h"

#include <sys/types.h>
#include <unistd.h>

JNIEXPORT jint JNICALL Java_POSIX_getpid (JNIEnv *env, jclass cls)
{
    return getpid ();
}

Now you can compile it using gcc:

$ gcc -Wall -I/usr/lib/jvm/java-1.6.0-sun-1.6.0.21/include -I/usr/lib/jvm/java-1.6.0-sun-1.6.0.21/include/linux -o libPOSIX.so -shared -Wl,-soname,libPOSIX.so POSIX.c -static -lc

You have to specify the location where your Java is installed. That's all. Now you can use it. Create a simple getpid program:

public class getpid
{
    public static void main (String argv[])
    {
        System.out.println (POSIX.getpid ());
    }
}

Compile it with javac getpid.java and run it:

$ java getpid &
[1] 21983
$ 21983

The first pid is written by the shell and the second is written by the Java program after shell prompt has returned. ∎

怎会甘心 2024-07-13 21:58:12

生成一个 shell 进程来读取其父进程的 pid。 那一定是我们的pid。 这是运行代码,没有异常和错误处理。

import java.io.*;
import java.util.Scanner;

public class Pid2
{
  public static void main(String sArgs[])
    throws java.io.IOException, InterruptedException
  {
    Process p = Runtime.getRuntime().exec(
      new String[] { "sh", "-c", "ps h -o ppid $" });
    p.waitFor();
    Scanner sc = new Scanner(p.getInputStream());
    System.out.println("My pid: " + sc.nextInt()); 
    Thread.sleep(5000);
  }
}

如果只是为了发出另一个 shell 命令而获取 PID,那么这个解决方案似乎是最好的。 将命令用反引号括起来就足够了,将其作为参数传递给另一个命令,例如:

nice `ps h -o ppid $`

这可能会替换给 exec 调用的数组中的最后一个字符串。

Spawn a shell process that will read its parent's pid. That must be our pid. Here is the running code, without exception and error handling.

import java.io.*;
import java.util.Scanner;

public class Pid2
{
  public static void main(String sArgs[])
    throws java.io.IOException, InterruptedException
  {
    Process p = Runtime.getRuntime().exec(
      new String[] { "sh", "-c", "ps h -o ppid $" });
    p.waitFor();
    Scanner sc = new Scanner(p.getInputStream());
    System.out.println("My pid: " + sc.nextInt()); 
    Thread.sleep(5000);
  }
}

This solution seems to be the best if the PID is to be obtained only to issue another shell command. It's enough to wrap the command in back quotes to pass it as an argument to another command, for example:

nice `ps h -o ppid $`

This may substitue the last string in the array given to exec call.

薆情海 2024-07-13 21:58:12

Java 9 最终通过 ProcessHandle

ProcessHandle.current().pid();

这:

  • 首先获取当前进程的 ProcessHandle 引用。

  • 为了访问其pid

无需导入,因为 ProcessHandlejava.lang 的一部分。

Java 9 finally offers an official way to do so with ProcessHandle:

ProcessHandle.current().pid();

This:

  • First gets a ProcessHandle reference for the current process.

  • In order to access its pid.

No import necessary as ProcessHandle is part of java.lang.

尘曦 2024-07-13 21:58:12

您可以尝试 JNR-Posix 中的 getpid() 。

它还有一个 Windows POSIX 包装器,可以调用 libc 的 getpid()。 不需要 JNI。

You can try getpid() in JNR-Posix.

It also has a Windows POSIX wrapper that calls getpid() off of libc. No JNI needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文