问题
I'm working on a Java application that utilises shutdown hooks in order to clean up on termination/interruption of the program, but I've noticed that Cygwin's implementation of CTRL-C doesn't seem to trigger the shutdown hooks. On the surface it appears to have interrupted the process, relinquishing control back to the command line, however the process' shutdown hooks are not triggered at all so cleanup does not occur.
In cmd they get caught, but due to various constraints I need to somehow get them working in Cygwin.
Is there any way to fire a SIGINT at a running process through Cygwin at all, or perhaps an alternative to shutdown hooks that I could use to clean up on interruption and termination?
回答1:
Bash invokes non-cygwin (windows) executables through an intermediate bash process (bash shell -> bash -> java). When you type Ctrl-C, the bash process gets a SIGINT and kills the child java process, so the shutdown hooks are not invoked. Windows processes are not aware of signals like SIGINT, SIGTERM or SIGKILL.
As described in the -Xrs option documentation, the java process registers a console control handler for the CTRL_C_EVENT windows event and that triggers a graceful termination that invokes the shutdown hooks. To get Ctrl-C to work as expected, you'd need to make sure the java process receives it, but right now that's intercepted by bash and not passed forward to the child process.
This issue can be restated as: How can I have Cygwin pass Ctrl-C forward to windows console processes?
Note: The bash behavior described above was verified on version 1.7.25.
回答2:
This thread perhaps shed some light. Some options
timeout 1d <command>
or
Ctrl-C patch by Howard Chu
来源:https://stackoverflow.com/questions/11757643/cygwin-ctrl-c-signal-interrupts-not-working-properly-jvm-shutdown-hooks-not