Search code examples
node.jsstdinpm2

Pass commands to nodejs server using pm2 module


I am running a nodejs server using pm2 module. I start my server on putty with the command line below :

pm2 start app.js

And I would like to pass "custom" commands to the server like quit; save; load. I am using stdin to do so, I have a code that looks like :

process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data', function (text) {
    if (text.trim() === 'quit') {
        // I do something here
    }
    if (text.trim() === 'save') {
        // I do another thing here
    }
    if (text.trim() === 'load') {
        // I do another thing here
    }
});

But this code doesn't work with pm2 module. The pm2 process crush the stdin process (I don't know if this is the right way to say that) only pm2 commands are listened.

So my question is : how can I pass my "custom" commands to the server ?

Thank you in advance


Solution

  • pm2 creates a subprocess to start you application. Because of this the stdin of the pm2 is not equal to your app. So unfortunately currently you can not use pm2 with stdin.

    There is however an issue open that plans on resolving this. https://github.com/Unitech/pm2/issues/2968

    The code is implemented and currently not in master. If you want this feature right now. You could go to the given commit and use that version of pm2 to do this.