如何在Java或Linux上找到PID ?

时间:2022-09-09 07:32:38

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.

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

Is there another way to obtain the PID?

是否有其他方法获得PID?

5 个解决方案

#1


25  

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

如果安装了procfs,您可以通过/proc/self symlink找到进程id,它指向一个名称为pid的目录(这里还有一些文件和其他相关信息,包括pid,但是在本例中,您只需要这个目录)。

Thus, with Java, you can do:

因此,使用Java,您可以:

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

In JRuby, you can use the same solution:

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

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

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

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

#2


7  

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

只在Linux中使用Sun JVM进行测试。可能与其他JMX实现不兼容。

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

#3


2  

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:

您可以使用JNI接口调用POSIX函数getpid()。这是非常直接的。您可以从您需要的POSIX函数的类开始。我把它叫做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

然后生成一个头文件POSIX。h和

$ 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:

头文件包含函数的C原型,并包装getpid函数。现在你必须实现这个函数,这很简单。我是用posix做的。

#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来编译它:

$ 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:

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

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

Compile it with javac getpid.java and run it:

用javac getpid编译它。java并运行:

$ 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. ∎

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

#4


2  

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.

生成将读取其父pid的shell进程。那一定是我们的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);
  }
}

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:

如果要获取PID而只发出另一个shell命令,那么这个解决方案似乎是最好的。它足以将命令放在后面的引号中,将其作为参数传递给另一个命令,例如:

nice `ps h -o ppid $$`

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

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

#5


0  

You can try getpid() in JNR-Posix.

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

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

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

#1


25  

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

如果安装了procfs,您可以通过/proc/self symlink找到进程id,它指向一个名称为pid的目录(这里还有一些文件和其他相关信息,包括pid,但是在本例中,您只需要这个目录)。

Thus, with Java, you can do:

因此,使用Java,您可以:

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

In JRuby, you can use the same solution:

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

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

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

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

#2


7  

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

只在Linux中使用Sun JVM进行测试。可能与其他JMX实现不兼容。

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

#3


2  

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:

您可以使用JNI接口调用POSIX函数getpid()。这是非常直接的。您可以从您需要的POSIX函数的类开始。我把它叫做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

然后生成一个头文件POSIX。h和

$ 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:

头文件包含函数的C原型,并包装getpid函数。现在你必须实现这个函数,这很简单。我是用posix做的。

#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来编译它:

$ 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:

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

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

Compile it with javac getpid.java and run it:

用javac getpid编译它。java并运行:

$ 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. ∎

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

#4


2  

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.

生成将读取其父pid的shell进程。那一定是我们的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);
  }
}

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:

如果要获取PID而只发出另一个shell命令,那么这个解决方案似乎是最好的。它足以将命令放在后面的引号中,将其作为参数传递给另一个命令,例如:

nice `ps h -o ppid $$`

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

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

#5


0  

You can try getpid() in JNR-Posix.

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

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

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