How to stop pm2 from killing detached child processes

Viewed 1573

pm2 is killing detached child processes on watch restart (ie which have been spawned with detached:true, stdio:'ignore', and child.unref().

Is there a way of telling pm2 not to kill the tree of child processes on restart?

3 Answers

The answer was to put the following in the ecosystem file (main section for app, not under the watch settings):

"treekill": false

I ended up using

pm2 restart myapp --no-treekill

and

pm2 stop myapp --no-treekill

I restart my nodejs app from within using the pm2 API. Here is the function I use so that detached child processes are not killed (notice the treekill: false option).

function restartPm2() {
    pm2.connect(function(err) {
        if (err) {
            console.error("PM2 FAILED TO CONNECT:", err);
        } else {
            pm2.restart('myapp.js', { treekill: false }, function() {});            
        }
    });

    pm2.disconnect();
}
Related