如何使用 SIGKILL Process.destroy() 在 java 中杀死 Linux 进程执行 SIGTERM [英] how can I kill a Linux process in java with SIGKILL Process.destroy() does SIGTERM
问题描述
在 Linux 中,当我在 java.lang.Process 对象(这是真正类型的 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?
推荐答案
不使用纯 Java.
最简单的替代方法是使用 Runtime.exec()
将 kill -9
命令作为外部进程运行.
Your simplest alternative is to use Runtime.exec()
to run a kill -9 <pid>
command as an external process.
不幸的是,要掌握 PID 并没有那么简单.您将需要使用反射黑魔法来访问 private int pid
字段,或者处理 ps
命令的输出.
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 - 实际上,还有另一种方式.创建一个小实用程序(C 程序、shell 脚本等)来运行真正的外部应用程序.对该实用程序进行编码,使其记住子进程的 PID,并为 SIGTERM 设置一个信号处理程序,用于 SIGKILL 子进程.
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.
这篇关于如何使用 SIGKILL Process.destroy() 在 java 中杀死 Linux 进程执行 SIGTERM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!