听听terminal会话“closures”事件
在terminal中,我启动后台进程A,进而启动进程B.进程B写入terminal(进程A已经传递了B正确的TTY文件描述符)。
我害怕的是,如果用户(在某些情况下,我)closuresterminal窗口,而不发送进程A或B SIGINT。 可能发生的是,即使用户closures了进程B仍然会尝试写入terminal。 更糟糕的是,用户可能会打开一个新的terminal窗口,它可能会假定与另一个terminal具有相同的身份/文件描述符,然后通过进程B写入。
基本上,我正在寻找一种方法来“terminal会话”的事件,如terminal会话被closures。
是否有可能在Node.js过程中监听这样的事件? 也许有一个相应的处理程序类似于process.on('SIGINT')
?
我猜测SIGTERM
事件可能是听到的事件,但现在在试验代码之后,不要以为是事实。
你应该寻找SIGHUP
(参见所有可能的信号 ):
var http = require('http'); var fs = require('fs'); var server = http.createServer(function (req, res) { setTimeout(function () { //simulate a long request res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }, 4000); }).listen(9090, function (err) { console.log('listening http://localhost:9090/'); console.log('pid is ' + process.pid); }); process.on('SIGHUP', function () { fs.writeFileSync(`${process.env.PWD}/sighup.txt`, `It happened at ${new Date().toJSON()}`); server.close(function () { process.exit(0); }); });