How to trap a SIGNAL in a java application initialized using a bash script

冷暖自知 提交于 2019-11-30 05:17:41

Works for me. Are you sure you are killing the right pid? On Unix you can use $! to get the pid of the last process you launched.

$ java -jar file.jar &
[1] 25573

$ kill -INT $!
Received SIGINT signal. Will teardown.

Update:

If you are launching this in the background via a shell script, the OS will create a new process group which will be immune to keyboard-generated signals such as SIGINT. Only processes whose process group ID matches the current terminal's process group ID can receive these signals.

So try running it within the current terminal's process group like this:

. ./script.sh

You will then be able to send SIGINT.

More information about job control here.

I going to guess the bash script is ignoring SIGINT and the child is inheriting the signal mask.

Look in the bash script for something like this. (There may be more numbers than just 2.)

trap "" 2

The empty string causes the shell to set SIG_IGN. If instead you use:

trap ":" 2

That registers a handler that does nothing. When a child process is started, any signal with a handler installed is reset to SIG_DFL.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!