如何使用 SIGKILL Process.destroy() 杀死 Java 中的 Linux 进程

发布于 2024-09-03 08:56:13 字数 131 浏览 5 评论 0原文

在Linux中,当我在 java.lang.Process 对象(这是 true 类型的 java.lang.UNIXProcess )上运行 destroy 函数时,它会发送一个 SIGTERM 信号来处理,有没有办法用 SIGKILL 杀死它?

In Linux when I run the destroy function on java.lang.Process object (Which is true typed java.lang.UNIXProcess ) it sends a SIGTERM signal to process, is there a way to kill it with SIGKILL?

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

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

发布评论

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

评论(4

此刻的回忆 2024-09-10 08:56:13

不使用纯Java。

最简单的替代方法是使用 Runtime.exec() 来运行 kill -9 命令作为外部进程。

不幸的是,获取 PID 并不是那么简单。您要么需要使用反射黑魔法来访问 private int pid 字段,要么弄乱 ps 命令的输出。

更新 - 实际上,还有另一种方法。创建一个将运行真正的外部应用程序的小实用程序(C 程序、shell 脚本等)。对实用程序进行编码,以便它记住子进程的 PID,并为 SIGTERM 设置一个信号处理程序,该处理程序将 SIGKILL 子进程。

Not using pure Java.

Your simplest alternative is to use Runtime.exec() to run a kill -9 <pid> command as an external process.

Unfortunately, it is not that simple to get hold of the PID. You will either need to use reflection black-magic to access the private int pid field, or mess around with the output from the ps command.

UPDATE - actually, there is another way. Create a little utility (C program, shell script, whatever) that will run the real external application. Code the utility so that it remembers the PID of the child process, and sets up a signal handler for SIGTERM that will SIGKILL the child process.

有深☉意 2024-09-10 08:56:13

斯蒂芬他的回答是正确的。我写了他说的:

public static int getUnixPID(Process process) throws Exception
{
    System.out.println(process.getClass().getName());
    if (process.getClass().getName().equals("java.lang.UNIXProcess"))
    {
        Class cl = process.getClass();
        Field field = cl.getDeclaredField("pid");
        field.setAccessible(true);
        Object pidObject = field.get(process);
        return (Integer) pidObject;
    } else
    {
        throw new IllegalArgumentException("Needs to be a UNIXProcess");
    }
}

public static int killUnixProcess(Process process) throws Exception
{
    int pid = getUnixPID(process);
    return Runtime.getRuntime().exec("kill " + pid).waitFor();
}

你也可以这样获取pid:

public static int getPID() {
  String tmp = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
  tmp = tmp.split("@")[0];
  return Integer.valueOf(tmp);
}

Stephen his answer is correct. I wrote what he said:

public static int getUnixPID(Process process) throws Exception
{
    System.out.println(process.getClass().getName());
    if (process.getClass().getName().equals("java.lang.UNIXProcess"))
    {
        Class cl = process.getClass();
        Field field = cl.getDeclaredField("pid");
        field.setAccessible(true);
        Object pidObject = field.get(process);
        return (Integer) pidObject;
    } else
    {
        throw new IllegalArgumentException("Needs to be a UNIXProcess");
    }
}

public static int killUnixProcess(Process process) throws Exception
{
    int pid = getUnixPID(process);
    return Runtime.getRuntime().exec("kill " + pid).waitFor();
}

You can also get the pid this way:

public static int getPID() {
  String tmp = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
  tmp = tmp.split("@")[0];
  return Integer.valueOf(tmp);
}
纵山崖 2024-09-10 08:56:13

如果您知道进程名称,则可以使用 pkill

Runtime.getRuntime().exec("pkill firefox").waitFor();

If you know process name you can use pkill

Runtime.getRuntime().exec("pkill firefox").waitFor();
小女人ら 2024-09-10 08:56:13

从Java 1.8开始,

你可以调用方法destroyForcously(),它默认调用destroy()方法,但根据Java文档,返回的所有子进程code>ProcessBuilderRuntime.exec() 实现此方法。

Since Java 1.8

you can call the method destroyForcibly(), which calls the destroy() method by default, but according to the Java docs, all sub-processes returned by ProcessBuilder or Runtime.exec() implement this method.

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