Exit MM2 process gracefully?

  • Module Developer

    I may be missing it somewhere, but does the core MM2 process call anything to clean up modules and exit gracefully?

    I was going to add a shutdown function to clean up some child processes started by my module, but was wondering if this functionality is already built into the core node-js app anywhere? This really only comes into play when restarting the MM process (without restarting the server itself).

    What I’m looking for would be something similar to the pseudo-code below:

    process.on('SIGINT', function() {
      modules.forEach( m => { 
        if (typeof m.exit === "function") {
      setTimeout(function() { process.exit(0); }, 300 );

  • Module Developer

    @shbatm Even if it does run some kind of cleanup routine when it closes, pm2 can’t know everything about what MagicMirror and the modules are doing, so writing a shutdown routine into your module can’t hurt.

    I’ve used the PubNub API in a couple of my modules and I run a routine on SIGINT to remove listeners and subscriptions. Pm2’s exit might take care of nullifying these or it might not, but with by writing the exit routine I can be sure these are taken care of.

  • Module Developer

    @j-e-f-f Thanks for the quick feedback!

    I was assuming I would need the cleanup routine in my module, I was just wondering if there was some routine in the core code that already listened for SIGINT and then called each module’s cleanup function (if there was one)–like it does with each node_helper.js’s start function–that way I would just have to add a function instead of add multiple SIGINT handlers (one in each module).

    It’s probably a niche requirement and it’s easy enough to add them for each module that needs one. Just curious if it existed, or was maybe worth adding.

  • Module Developer

    I ended up opening an issue related to this on GitHub here: #1056. I was trying to implement the method @j-e-f-f suggested, but when using Electron, it ignores the call to process.on('SIGINT'...).

    What I am suggesting, in case anyone else developing a module needs this: allow module developers to implement a stop: function() { } in their node_helper.js file that performs any cleanup required. When exiting, the MM app will call this function, if it exists, and each module can gracefully exit on it’s own.

    There is a working example of this here: shbatm/MagicMirror@9935d2f

  • Module Developer

    @shbatm what problems did you run in to? I’m using process.on('SIGINT'...) in two of my modules and they both get called called on MM exit.

    Here’s an example from my MMM-MyWink module:

    socketNotificationReceived: function(notification, payload){
        var self = this;
        if (notification === 'MMM-MYWINK-GET-INITIAL-STATUS') {
          // ... edited for brevity
          } else {
            this.config = payload;
            //log into Wink account and set up subscriptions
            //set up some cleanup on application exit
            process.on('SIGINT', function () {  
              console.log("[MMM-MyWink] Cleaning Up");

    Full code here:

  • Module Developer

    @j.e.f.f Just curious, are you running MM as server-only or with Electron (npm start)? My problem is I used almost exactly the same snippet you have and it was completely ignored when using Electron; however, it worked fine when using server-only mode. I did some research and found that the Electron browser intercepts the exit commands and needs an app.on("before-quit"... command, but I couldn’t override this from a module.

    I ended up making a tweak to the core and a pull request to work around this:

    If this gets incorporated then all you’d need in your node_helper.js is a stop() function:

    stop: function() { 
      console.log("[MMM-MyWink] Cleaning Up");

  • Module Developer

    @shbatm I have that code working with Electron running. I’m running it on the server side in node_helper.js, not in he browser-side main module file. Maybe that’s the difference?

  • Module Developer

    @j-e-f-f, very strange. Mine was in the node-helper file as well, called inside the start function and I also tried it in the core js/app.js file with the same result. As I mentioned, it worked fine for server only and I was pulling my hair out trying to get it to work when running MM normally… Ctrl+C when running npm start and pm2 stop mm with pm2 would exit without even writing the console line.

    Full code that wasn’t working here

    Working code based on the change request I described here

    Thank you for the help, I’m going to chalk it up to a fluke that I’ve spent too much time investigating and now have a work around. It’ll be nice to have a standard clean-up method incorporated in the main code anyways.

  • Module Developer

    @shbatm very strange indeed! The only thing I did differently is I didn’t set up the listener in the start() function, but in the socketNotificationReceived() routine. Maybe the difference is that I’m setting up the listener after Electron has started. The start() function in node_helper.js might execute before Electron starts?

    I like your stop() function idea but I worry that it won’t be called in the event of an MM crash, in which case anything that should otherwise have run in the stop() routine won’t get called. Listening for SIGINT is a more reliable option in that situation, especially if you have resources that need to be explicitly released.

  • Module Developer

    @j.e.f.f That could be, I wonder if when Electron starts it somehow removes any SIGINT listeners already created.

    With respect to running stop() during a MM crash – the main function in js/app.js is still attaching to process.on("SIGINT", ...) just upstream of the individual node_helper files. If MM crashes I think it should still call the stop() functions unless the node_helper array gets corrupted. An advantage to cycling through each node_helper would be that you don’t have multiple individual SIGINT listeners that are trying to call a process.exit before the other is done.