I have a Java app that creates an external process and reads the process' stdout through an InputStream. I need to be able to kill the process when I am done with it. Is there a way to send a SIGINT signal to this process? (as if I pressed Ctrl+C from the console).
The external process is not my code and I cannot modify it.
Can you send kill -SIGINT <pid>
to the process (given that you know the process ID):
Runtime.getRuntime().exec("kill -SIGINT 12345");
Of course, that would make for a platform-dependent solution... Potentially, you'll be able to use this tool, although it is in "sandbox mode". But it might give you an idea:
http://commons.apache.org/sandbox/runtime/
See also this related question here:
how can I kill a Linux process in java with SIGKILL Process.destroy() does SIGTERM
For other people arriving here from Google if you want to send the signal to the current Process (aka JVM) you can use sun.misc.Signal.raise()
method. Personally I need it because of a JNI library that I am using.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With