Search code examples
node.jsgruntjscommand-line-interfacedaemonepub

How can I run grunt as a daemon?


I am running a packaged nodejs webserver that allows for reading of epub files (Readium-JS), and it is started with the grunt command.

However, if I run this on my VPS the server dies as soon as my terminal connection ends.

How can I run this task as a daemon?

I have looked at options like grunt-forever and grunt-daemon but the way the Gruntfile is written using load-grunt-config is messing with my mind and I can't piece together how to isolate the server code.


Solution

  • Here's the solution I found:

    As was suggested above, using pm2

    However, when I ran

    pm2 start grunt 
    

    I got an error saying that the grunt module did not exist, which was weird.

    So I ended up writing a script which worked:

    -- start.js --

    var pm2 = require('pm2');
    
    pm2.connect(function() {
      pm2.start({
        script    : '/usr/local/bin/grunt',         // Script to be run
        args: '--force',
      }, function(err, apps) {
        pm2.disconnect();
      });
    });
    

    After running node start.js from the command line, everything sailed smoothly.