开发者

How to pass SIGINT to a process created in java?

I've created a process using Runtime.exec, and I need a way of killing the process as one would using Ctrl+C. The process I've started is a video recording tool that specifically looks for a keyboard interrupt in order to write the video file and close all connections cleanly. Unfortunately, Process.destro开发者_如何学Cy doesn't do this. Is there any other way of doing it in java, using the handle I have of the process?


You basically need to do a kill command on the process to send a signal. I don't think Java has a way to do that directly. You may be able to Runtime.exec() the kill command itself.


I can think of a not-too-clean solution, for instance writing a small shell script that invokes kill -2 `pidof executable` (or killall -2 executable if you do not mind killing every process). A much cleaner solution would be to retrieve the process ID from java, but I do not know how (or if) it can be done.


You should be kill the process when the Java gets killed using following!

Process process = Runtime.getRuntime().exec();  
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {  
    public void run() {  
            process.destroy();  
        }  
}));  
process.waitFor(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜