使用bash脚本在Java应用程序中的信号如何捕获初始化 [英] How to trap a SIGNAL in a java application initialized using a bash script

查看:150
本文介绍了使用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.

有关作业控制这里更多信息

这篇关于使用bash脚本在Java应用程序中的信号如何捕获初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆