Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

pm2

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?

like image 844
ape_face Avatar asked Jan 21 '26 06:01

ape_face


1 Answers

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

like image 79
madflow Avatar answered Jan 22 '26 19:01

madflow