使用bash脚本在Java应用程序的信号捕获初始化
问题说明
我用下面的code追赶Java中的INT信号:
I am catching an INT signal in java using the following code:
Signal.handle(new Signal("INT"), new SignalHandler () {
public void handle(Signal sig) {
log.warn("Received SIGINT signal. Will teardown.");
task.tearDown();
// Force exit anyway
System.exit(1);
}
});
当我使用 Java的罐子file.jar
启动我的应用程序能赶上送与信号杀死-INT PID
。
When I am using java -jar file.jar
to start my application I can catch the signal sent with with kill -INT PID
.
如果我称之为 Java的罐子file.jar&安培;
(JVM在后台运行),我不能赶上与发送的信号杀-INT
。
If I call java -jar file.jar &
(jvm runs in the background), I can't catch the signal sent with kill -INT
.
任何想法?
感谢。
正确答案
我的作品。你确定你正在扼杀正确的PID?在Unix上你可以使用 $!
让你推出的最后进程的PID。
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.
更新:
如果您在通过shell脚本在后台启动此,操作系统将创建这将是免疫键盘产生的信号,例如SIGINT一个新的进程组。只有其进程组ID匹配当前终端的进程组ID可以接收这些信号。
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
您届时可发送SIGINT。
You will then be able to send SIGINT.
有关作业控制这里更多信息
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfjhik
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20