Search code examples
node.jsprocessrestartpm2

NodeJS PM2 - Programmatically restart process using PM2 API


I want to programmatically restart a process using PM2 API in NodeJS. From their docs, I learned that you can do this by calling pm2.restart(process, errback). Thing is that it is not said anywhere what type the process argument must be.
Does it have to be the name of the process? Or its PM2 process ID? Or a PM2 process object?


Solution

  • Did a quick test, name of the process should work, for example:

    var pm2 = require('pm2');
    
    pm2.connect(function(err) {
      if (err) {
        console.error(err);
        process.exit(2);
      }
    
      pm2.restart('timer', function(err) {
        pm2.disconnect();   // Disconnects from PM2
        if (err) throw err
      });
    });
    
    

    Where timer is the name of the process started with pm2.