CTRL-C doesn't work on Java program
I found a weird scenario where if I start a java program and I want to exit gracefully with CTRL+C it doesn't work/respond, I have to do a CTRL+Z 开发者_高级运维on the program and this is not cool, doing a ps lists the process... Anyone please.
Under Unix, command line programs have a lot of control over what happens when you try to interrupt them with ^C
. The default effect of typing ^C
at a terminal is to make the kernel send the SIGINT
signal to the foreground process group, and the default behavior of SIGINT
is to kill the process it's sent to, but both of those things can be changed.
The most likely cause of your problem is that your Java program is intercepting SIGINT
in order to do some cleanup before exiting, but the signal handler is buggy so the process doesn't ever actually exit. The second most likely cause is that the program is ignoring SIGINT
altogether. And the least likely cause is that it's put the terminal into "raw mode" so that ^C
just delivers a byte with value 0x03 to its standard input (if it had done that ^Z
probably wouldn't work either).
If you have access to the source code of your program, you can try to fix the buggy signal handler and/or make it stop ignoring the signal. Otherwise, you're kinda up a creek. You can try ^\
(control-backslash), which sends a different normally-lethal signal (SIGQUIT
), but this is not guaranteed to work either, and if it does work, it may leave you with a gigantic "core dump" file to get rid of.
The only 100% for sure way to get rid of a malfunctioning process is to send it signal 9 (SIGKILL
). Unlike the other lethal signals, it's impossible to intercept, block, or ignore that one. There is no control key to send signal 9; you have to suspend the process with ^Z
, or open a new terminal window / ssh session, find the process ID with ps
, and use the kill
command. Always try kill PID
before kill -9 PID
.
I had the same problem (In my case it was problem :)), my issue was that I launched the app using javaw
instead of java
.
javaw
is running the app without console so the CTRL+C is not working.
Another question about the difference between javaw
to java
here - What is the difference between 'java', 'javaw', and 'javaws'?
精彩评论