【问题标题】:What event emits in Node.js for an app exited due to SSH session ending? (Need to cleanup GPIO)由于 SSH 会话结束而退出的应用程序在 Node.js 中发出什么事件? (需要清理GPIO)
【发布时间】:2018-09-29 01:26:14
【问题描述】:

我正在运行一个执行一些 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 会话终止而关闭时会发出什么事件?

【问题讨论】:

  • 通常是 SIGHUP,您是否尝试监听该信号?
  • @mscdex 感谢您的回复,不幸的是,这似乎并没有改变任何事情:(您确实将我带到了nodejs.org/api/process.html#process_signal_events,我将阅读其中的一些内容并添加更多内容进行测试和看看它们是否也能工作。

标签: node.js events ssh gpio interrupt-handling


【解决方案1】:

对不起,答案不是很好,但我不确定其中哪一个是它起作用的原因(或者它是否是其中许多的组合,因为在运行应用程序时 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    相关资源
    最近更新 更多