如何使用SIGKILL杀死java中的Linux进程Process.destroy()执行SIGTERM [英] how can I kill a Linux process in java with SIGKILL Process.destroy() does SIGTERM

查看:348
本文介绍了如何使用SIGKILL杀死java中的Linux进程Process.destroy()执行SIGTERM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中,当我在java.lang.Process对象上运行destroy函数(这是真正的类型java.lang.UNIXProcess)时,它会发送一个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< pid> 命令作为外部进程。

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.

更新 - 实际上,还有另一种方法。创建一个运行真实外部应用程序的小实用程序(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杀死java中的Linux进程Process.destroy()执行SIGTERM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆