• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用bash脚本在Java应用程序的信号捕获初始化

用户头像
it1352
帮助1

问题说明

我用下面的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.

任何想法?

感谢。

正确答案

#1

我的作品。你确定你正在扼杀正确的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
系列文章
更多 icon
同类精品
更多 icon
继续加载