Node.js进程和退出代码没有显示

sig.js

process.on("exit", function(code){ process.stdout.write("it was: " + code); }); setTimeout(function(){ console.log("times up"); }, 30000); 

如果我然后运行

nohup node sig.js &

让时钟用完,我得到了预期的nohup.out文件

 it was: 0 

但是如果我使用kill -SIGINT <process id>那么我得到一个完全为空的nohup.out文件。 基于文档https://nodejs.org/api/process.html#process_event_exit我期待着什么 …我在这里错过了什么?

从链接的文档:

该事件仅在Node.js由process.exit()显式退出或由事件循环泄露隐式发出时才会发出。

当您发送SIGINT时,在退出事件被触发之前该进程被终止。

您需要显式侦听SIGINT事件:

 process.on("SIGINT", function () { //graceful shutdown process.exit(); }); 

这将捕获SIGINT并执行正常关机,这将迫使它通过您的预期的代码。