如何使用 SIGKILL Process.destroy() 在 java 中杀死 Linux 进程执行 SIGTERM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2950338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:37:50  来源:igfitidea点击:

how can I kill a Linux process in java with SIGKILL Process.destroy() does SIGTERM

javalinuxprocesskillsigterm

提问by ekeren

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?

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

采纳答案by Stephen C

Not using pure Java.

不使用纯Java。

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

你最简单的选择是使用Runtime.exec()运行kill -9 <pid>的外部进程的命令。

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 pidfield, or mess around with the output from the pscommand.

不幸的是,要掌握 PID 并不是那么简单。您要么需要使用反射黑魔法来访问该private int pid字段,要么需要处理ps命令的输出。

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.

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

回答by Martijn Courteaux

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:

您还可以通过以下方式获取 pid:

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

回答by user4757345

If you know process name you can use pkill

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

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

回答by Ercksen

Since Java 1.8

从 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 ProcessBuilderor Runtime.exec()implement this method.

您可以调用该方法destroyForcibly(),该destroy()方法默认调用该方法,但根据Java 文档,所有子进程返回ProcessBuilderRuntime.exec()实现该方法。