简体   繁体   English

当我退出我的 Node 应用程序时,我可以运行 function 吗?

[英]Can I run a function when I quit my Node App?

I'm working on a project for school in Node.js on a Raspberry Pi.我正在 Node.js 在 Raspberry Pi 上为学校做一个项目。 I have a script that is running and will run for long periods of time controlling some LEDs.我有一个正在运行的脚本,它将长时间运行以控制某些 LED。 Is there a way that I can run a function when I quit the program to turn off all the LEDs I'm using (Stop power to the GPIO)?有没有办法在我退出程序时运行 function 以关闭我正在使用的所有 LED(停止为 GPIO 供电)?

You can handle the SIGINT (CTRL-C) signal.您可以处理SIGINT (CTRL-C) 信号。 Something like:就像是:

function cleanup() {
  // do clean up here
  console.log("clean up");
}

process.on("SIGINT", () => {
  cleanup();
  process.exit(0);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试运行我的节点 app.js 时,我的命令行出错 - Error in my command line when i tried to run my node app.js 当满足某些条件时,如何通过一堆等待退出 javascript function - How can I quit from a javascript function with a bunch of awaits when certain condition is met 当我运行我的反应应用程序时,我得到错误 default.storage is not a function - I'm gettin error default.storage is not a function when i run my react app 如何在Facebook即时游戏中检查App退出? - How can I check App quit in the facebook instant games? 为什么不能在端口3000上运行此节点应用程序? - Why can't I run this node app on port 3000? 如何在节点js中运行多个应用程序? - How can i run more than one app in node js? 如何在更改值而不是单击按钮时运行函数? - How can I make my function run when a value is changed instead of when the button is clicked? 如何在节点服务器上正确运行ajax? - How can I properly run ajax on my node server? Bootstrap手风琴在Angular应用中打开或关闭时,如何运行某些代码? - How can I run some code when a Bootstrap accordion opens or closes in my Angular app? 为什么我的应用程序在运行时出现此错误,“ERROR in./node_modules/@nestjs/common/cache/cache.providers.js”? - Why is my app getting this error when I run it, “ERROR in ./node_modules/@nestjs/common/cache/cache.providers.js”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM