How to send SIGINT signal from Java to an external process?
Asked Answered
S

4

24

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.

Shellans answered 20/10, 2011 at 11:25 Comment(0)
A
2

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

Articulation answered 20/10, 2011 at 11:54 Comment(1)
That seems to send a SIGTERM signal, not SIGINT. See this question here: #2950838Balsa
B
18

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

Balsa answered 20/10, 2011 at 11:47 Comment(7)
There's no platform-independent solution as Windows has no Signals.Russom
@fix_moeller: I guess you're right. But maybe there's a library providing some sort of abstraction, at least for killing other processes...Balsa
@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????Galagalactagogue
@SpaceRocker: The best way to get answers for your question is to ask a new question on Stack OverflowBalsa
@LukasEder, if i ask this question, it will be flaged as inappropriate, StackOverflow is biased towards me unfortunately :(Galagalactagogue
@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...Balsa
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())Jeanejeanelle
F
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.

Fix answered 12/4, 2012 at 0:49 Comment(3)
That doesn't seem to send an actual signal, only call the signal handler. Or?Recalcitrant
@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.Blamed
The signals are platform-specific. For example, on Windows, only a subset of signals can be raised.Polydeuces
W
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.

Weylin answered 20/10, 2011 at 11:53 Comment(0)
A
2

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

Articulation answered 20/10, 2011 at 11:54 Comment(1)
That seems to send a SIGTERM signal, not SIGINT. See this question here: #2950838Balsa

© 2022 - 2024 — McMap. All rights reserved.