0

I'm working on a project for school in Node.js on a Raspberry Pi. I have a script that is running and will run for long periods of time controlling some LEDs. 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)?

  • 2
    Does this answer your question? [Doing a cleanup action just before Node.js exits](https://stackoverflow.com/questions/14031763/doing-a-cleanup-action-just-before-node-js-exits) – 001 Feb 07 '22 at 16:17

1 Answers1

1

You can handle the SIGINT (CTRL-C) signal. Something like:

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

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