我正在运行正在执行一些GPIO工作的Node.js应用程序。如果发生任何类型的错误,或者用户使用CTRL + C
关闭了应用程序,我目前正在处理它。我无法捕获的一种情况是,通过关闭SSH客户端断开SSH会话。
关闭我的SSH客户端会杀死Node.js应用程序(这很好),但是在关闭它之前,需要执行某些GPIO清除方法。
这些是我尝试过的事件,但是在SSH关闭时都没有捕获到这些事件:
var cleanup = () => { /*cleanup code*/ };
var domain = require('domain').create();
domain.on('error', cleanup);
process.on('uncaughtException', cleanup);
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
process.on('exit', cleanup);
process.on('unhandledRejection', cleanup);
process.on('disconnect', cleanup);
process.on('SIGUSR1', cleanup);
process.on('SIGUSR2', cleanup);
process.on('beforeExit', cleanup);
应用程序由于SSH会话终止而关闭时,会发出什么事件?
答案 0 :(得分:0)
很抱歉,您的答案不是很好,但是我不确定其中哪个是它起作用的原因(或者是否是其中许多原因的组合,因为在运行应用程序时SSH会话关闭时会触发多个信号),但我登陆的最终名单是:
process.on('SIGINT', cleanup);
process.on('SIGHUP', cleanup);
process.on('SIGTERM', cleanup);
process.on('SIGCONT', cleanup);
现在,在运行程序时退出SSH会话可正确运行清除方法。
对于那些好奇我所有错误处理代码实际上是什么样的人
var domain = require('domain').create();
domain.on('error', cleanup);
process.on('unhandledRejection', cleanup);
process.on('uncaughtException', cleanup);
process.on('SIGINT', cleanup); // Handle CTRL + C, *nix only https://stackoverflow.com/a/20165643/231730
process.on('SIGHUP', cleanup); // SSH hangup (probably needed with SIGCONT)
process.on('SIGTERM', cleanup); // Handles `kill PID` command on linux
process.on('SIGCONT', cleanup); // Handle when SSH connection closes that was running the app