- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在我的 Node 应用程序中,我挂上了 SIGINT
信号以便正常停止(使用 pm2
,但这与此处无关)。
我的应用还执行/生成了几个子进程。
我能够挂上 SIGINT
来拦截它并执行正常停止,然而我的子进程通过相同的信号传递,因此被立即杀死。
如何拦截子进程上的 SIGINT
信号?
我正在做的一个示例:
const child = child_process.spawn('sleep', ['10000000']);
console.log(`Child pid: ${child.pid}`);
child.on('exit', (code, signal) => { console.log('Exit', code, signal); });
process.on('SIGINT', () => {
console.log("Intercepting SIGINT");
});
最佳答案
默认情况下,由 child_process.spawn()
创建的子进程具有相同的 process group作为父级,除非使用 {detached:true}
option 调用它们.
结果是这个脚本在不同的环境中会有不同的表现:
// spawn-test.js
const { spawn } = require('child_process');
const one = spawn('sleep', ['101']);
const two = spawn('sleep', ['102'], {detached: true});
two.unref();
process.on('SIGINT', function () {
console.log('just ignore SIGINT');
});
在交互式 shell 上,默认情况下,来自 Ctl-C 的 SIGINT 会发送到整个组,因此未分离的 child 将获得 SIGINT 并退出:
you@bash $ node spawn-test.js
^Cjust ignore SIGINT
# the parent process continues here, let's check children in another window:
you@bash [another-terminal-window] $ ps aux | grep sleep
... sleep 102
# note that sleep 101 is not running anymore
# because it recieved the SIGINT from the Ctl-C on parent
...但是对 kill(2)
的调用只能向您的父进程发出信号,因此子进程可以存活:
you@bash $ node spawn-test.js & echo $?
[2] 1234
you@bash [another-terminal-window] $ kill -SIGINT 1234
you@bash [another-terminal-window] $ ps aux | grep sleep
... sleep 101
... sleep 102
# both are still running
然而,pm2 完全是另外一头野兽。即使您尝试上述技术,它也会杀死整个进程树,包括您的分离进程,即使是很长的 --kill-timeout
:
# Test pm2 stop
you@bash $ pm2 start spawn-test.js --kill-timeout 3600
you@bash $ pm2 stop spawn-test
you@bash $ ps aux | grep sleep
# both are dead
# Test pm3 reload
you@bash $ pm2 start spawn-test.js --kill-timeout 3600
you@bash $ pm2 reload spawn-test
you@bash $ ps aux | grep sleep
# both have different PIDs and were therefore killed and restarted
这似乎是 pm2 中的一个错误。
我已经通过使用 init 系统(在我的例子中是 systemd)而不是 pm2 解决了类似的问题,因为这样可以更好地控制信号处理。
在 systemd 上,默认情况下将信号发送到整个组,但您可以使用 KillMode=mixed
将信号仅发送到父进程,但如果它们运行超出,仍会 SIGKILL 子进程超时。
我的 systemd 单元文件如下所示:
[Unit]
Description=node server with long-running children example
[Service]
Type=simple
Restart=always
RestartSec=30
TimeoutStopSec=3600
KillMode=mixed
ExecStart=/usr/local/bin/node /path/to/your/server.js
[Install]
WantedBy=multi-user.target
关于node.js - Node 子进程 : how to intercept signals like SIGINT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44788013/
脚本1.sh: #!/bin/bash ./script2.sh echo after-script 脚本2.sh: #!/bin/bash function handler { exi
我正在编写一个多线程程序,我想在其中处理来自用户的可能的 Ctrl-C 命令以终止执行。据我所知,不能保证能够取消每个工作线程的主线程会捕获信号。因此,是否有必要为工作线程的代码设置一个不同的信号处理
我正在编写一个多线程程序,我想在其中处理来自用户的可能的 Ctrl-C 命令以终止执行。据我所知,不能保证能够取消每个工作线程的主线程会捕获信号。因此,是否有必要为工作线程的代码设置一个不同的信号处理
标题说明了一切。我该如何处理或捕捉 SIGINT在 Julia ?从文档中我假设我只是想 catch InterruptException使用 try/catch像下面这样块 try whil
以下是此问题的简单重现: void handler(int sig){ if(sig == SIGINT){ printf("Signal caught
鉴于此代码: #include #include #include void sigint_handler(int h) { printf("Hey! I caught a SIGINT
我正在写一个shell,现在它来控制子进程。当我在子进程中使用 signal (SIGTERM, SIG_DFL); 时, 信号 SIGINT 由 Ctrl + C 生成,该信号终止整个操作系统 sh
我正在尝试 SIGINT。我基本上希望我的程序在用户按下 control-c 时立即启动。当这种情况发生时,我将让程序制作一个管道。 现在,我决定当在信号处理程序中按下 control-c 时,它将调
我想处理来自内核的 SIGINT 信号,以便调用一个函数来优雅地关闭我的进程。 这是一个工作示例,一个线程正在等待信号,然后调用函数 handle_stop: #include #include
我有一个 bash 脚本,它启动了 2 个进程: openocd ...flags... 2>openocd.log & arm-none-eabi-gdb 在 gdb 中,使用 Ctrl+C 中断执
我有几个带有 while read line 循环的脚本,当我按下 Ctrl C 时,它们不执行我的清理功能。例如: #!/bin/bash cleanup() { stty echo exi
我想捕获从 Script-A.sh 发送到 Script-B.sh 的信号所以在 Script-A.sh 中我使用命令: (Send SIGINT to Script-B.sh) kill -2 $P
我正在尝试处理 SIGINT。 SIGINT 在我的程序中的主要目的是取消当前搜索功能并打印当前可用的结果。但是每当我 try catch SIGINT 信号时,它都会关闭我的程序。 (我搜索了这么多
我有 2 个程序(由我编写)。第一个名为“MAN”的进程将调用“D”(第二个),这是一个将在后台运行直到以某种方式终止的进程。 我想在不终止 D 的情况下终止 MAN。 我尝试使用 ctrl + c
我应该编写一个 C 程序来处理第一个带有自定义处理程序的 SIGINT,然后重置默认行为。我的自定义 SIGINT 处理程序应该只打印一条消息。这是我写的: #include #include #
如果父进程接收到 ctrl-C 信号,我一直试图阻止我的父线程杀死子进程。我已经没有想法了。 父进程已经捕捉到 SIGINT,所以我想要和现在 child 死了一样。 int main() {
我对Python很陌生,所以如果这个问题非常基本,请原谅我。 我试图在使用选择模块从套接字接受数据时处理键盘中断。因此,我有一个 select.select() 函数调用来等待来自套接字的数据,然后将
我无法理解为什么我的 SIGINT 从未被下面的代码捕获。 #!/usr/bin/env python from threading import Thread from time import sl
我正在编写以下代码。该程序应该能够使用 sigaction 处理 SIGINT。到目前为止,它几乎完成了,但我遇到了两个问题。 第一个问题是如果程序在 3 秒内收到 3 个信号,它应该打印“正在关闭”
在 C 中,我想捕获 SIGINT 信号并打印出类似这样的消息通过使用 sigaction 并通过 向其传递新的处理程序来“接收到 SIGINT” sa.sa_sigaction = handler;
我是一名优秀的程序员,十分优秀!