23

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.

royhowie
  • 11,075
  • 14
  • 50
  • 67
Jjreina
  • 2,633
  • 6
  • 33
  • 54

4 Answers4

17

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

Community
  • 1
  • 1
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • 3
    There's no platform-independent solution as [Windows has no Signals](http://stackoverflow.com/questions/140111/sending-an-arbitrary-signal-in-windows). – Thunraz Oct 20 '11 at 11:51
  • @fix_moeller: I guess you're right. But maybe there's a library providing some sort of abstraction, at least for killing other processes... – Lukas Eder Oct 20 '11 at 11:58
  • @Lukas Eder, i have a question, if i open this process in a new thread, and then lets say it is hanged etc, and i kill it using SIGINT in linux (from another thread), it basically kills my whole java program, is there any possibility that only this particular process is killed while my program is still running???? – Space Rocker Mar 15 '13 at 11:02
  • @SpaceRocker: The best way to get answers for your question is to ask a new question on Stack Overflow – Lukas Eder Mar 15 '13 at 18:29
  • @LukasEder, if i ask this question, it will be flaged as inappropriate, StackOverflow is biased towards me unfortunately :( – Space Rocker Mar 16 '13 at 02:15
  • @SpaceRocker: What makes you think so? It takes time to understand *how* to ask questions here. But I don't think your experience (from looking at your profile) is particularly bad... – Lukas Eder Mar 16 '13 at 09:40
  • If you need to send a signal to yourself (e.g. to generate stack trace) you can do `Runtime.getRuntime().exec("kill -SIGQUIT " + ProcessHandle.current().pid())` – David Tinker Nov 17 '22 at 09:33
6

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.

Jason Axelson
  • 4,485
  • 4
  • 48
  • 56
  • That doesn't seem to send an actual signal, only call the signal handler. Or? – dkagedal Dec 19 '12 at 11:16
  • @Jason Axelson I am also using sun.misc.Signal.raise(new Signal("TERM")); I am initializing a JNI component which keeps running and never stops, even my other threads are stopped . so i am using this solution to shutdown my application. – vijayashankard Nov 17 '15 at 10:39
  • The signals are platform-specific. For example, on Windows, only a subset of signals can be [raised](https://msdn.microsoft.com/en-us/library/dwwzkt4c.aspx). – rustyx May 17 '18 at 12:39
2

Unfortunately there isn't a native way to send an arbirtray signal in Java.

I agree with Lukas in using Runtime.exec() or you could use something like Posix for Java library.

D. Cannone
  • 78
  • 5
2

Are you running the external program as a java.lang.Process? As the Process class has a destroy() method.

frogatto
  • 28,539
  • 11
  • 83
  • 129
Ulf Jaehrig
  • 749
  • 5
  • 11
  • 18
    That seems to send a SIGTERM signal, not SIGINT. See this question here: http://stackoverflow.com/questions/2950338/how-can-i-kill-a-linux-process-in-java-with-sigkill-process-destroy-does-sigte – Lukas Eder Oct 20 '11 at 11:58