[英]Graceful kill off a detached node.js spawned child process in Windows
不是这个的重复:我可以很好地杀死进程,我想知道如何在进程中检测到它正在被杀死,然后优雅地关闭。
我有一个 CLI 工具来生成和终止子进程 node.js。 演示代码包含在这三个文件中:
spawn.js
将生成child.js
脚本,分离。 为简单起见,我 pipe 孩子的stdio
到一个out.log
文件
child.js
-- 一个写入文件的简单计数器,使用readline
方法检测 Windows 中的模拟 SIGINT
kill.js
-- 在子进程上调用process.kill()
,使用它的 PID
spawn.js
'use strict';
var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');
var childFilePath = path.resolve(__dirname, 'child.js');
var out = fs.openSync('./out.log', 'a');
var err = fs.openSync('./out.log', 'a');
var options = {
detached: true,
stdio: ['ignore', out, err],
};
var child = spawn(process.execPath, [childFilePath], options);
child.unref();
孩子.js
'use strict';
var fs = require('fs');
var path = require('path');
if (process.platform === 'win32') {
console.log('win32 true');
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('SIGINT', function() {
process.emit('SIGINT');
});
}
process.on('SIGINT', function() {
console.log('SIGINT');
process.exit();
});
var filepath = path.resolve(__dirname, 'pid.txt');
fs.writeFile(filepath, process.pid);
var i = 0;
setInterval(function () {
console.log(i++);
}, 1000);
kill.js
'use strict';
var fs = require('fs');
var path = require('path');
var pidPath = path.resolve(__dirname, 'pid.txt');
fs.readFile(pidPath, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
process.kill(data, 'SIGINT');
});
发送process.kill(PID, 'SIGINT')
时,它实际上并没有将其检测为 windows 中的SIGINT
。我可以手动运行child.js
并使用CTRL+C
终止进程以触发SIGINT
,所以我知道readline
代码正在工作(或者可能不是,因为SIGINT
将在没有readline
代码的情况下触发,但它会 n.netheless 触发SIGINT
)
process.kill()
不会将信号类型发送到分离进程吗? 我如何检测到一个单独的脚本试图终止我的子进程并正常关闭?
我也有完全一样的问题。 我注意到rl.on("SIGINT")
不起作用但rl.on("close")
有效!
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
})
rl.on('close', function() {
process.emit('SIGINT')
})
这是我的调试输出
Sat Feb 17 2018 11:43:28 GMT+0800 (China Standard Time) process started (pid=6920)
Sat Feb 17 2018 11:43:28 GMT+0800 (China Standard Time) SIGINT captured! cleanup and then call process.exit(0)
Sat Feb 17 2018 11:43:28 GMT+0800 (China Standard Time) byebye! (code=0, signal=undefined)
请注意,我不太确定向子进程添加额外的代码是个好主意。 考虑分叉非nodejs进程,它可能无法使用这样的技巧(例如redis-server,kdb +)。 我仍在寻找一种方法让spawn.js
优雅地杀死子spawn.js
。
更新1:这是我向PM2社区报告的初始问题https://github.com/Unitech/pm2/issues/3467
有一个库kill-with-style可以执行此操作,并且也可以与分离进程一起使用。 它有多种选项来设置信号、超时和重试选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.