Search code examples
node.jspm2

How to run multiple pm2 instances each with different cmd args?


I have a whole bunch of files to parse and transform and I've created a nodejs app for this where I supply stuff like source and target dir as cmd args when I run the app. Now I would like to start a whole bunch of these processes each with different cmd args. With npm I have to do this manually. Can I do this programmatically with pm2? Can I say to pm2 run 10 instances of my app, each instance takes different cmd args?


Solution

  • There is the concept of an ecosystem file.

    A minimal example would be:

    // worker.js
    setInterval(function() {
      console.log(process.argv);
    }, 1000);
    
    // ecosystem.config.js
    module.exports = {
      apps: [
        {
          name: 'Worker 1',
          script: 'worker.js',
          args: 'one two'
        },
        {
          name: 'Worker 2',
          script: 'worker.js',
          args: 'three four'
        }
      ]
    };
    

    In the same directory simply execute: pm2 start