Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send SIGINT signal from Java to an external process?

Tags:

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.

like image 775
Jjreina Avatar asked Oct 20 '11 11:10

Jjreina


2 Answers

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

like image 161
Lukas Eder Avatar answered Sep 21 '22 13:09

Lukas Eder


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.

like image 23
Jason Axelson Avatar answered Sep 21 '22 13:09

Jason Axelson