PM2 Common Settings For All Processes in JSON File

Viewed 303

I am currently starting multiple Node.js processes using pm2. These processes are defined in a JSON file, which is used by running

pm2 start pm2.json

As you can see from the JSON file below, there is a lot of repetition. For example, making a change to the error_file location or setting the variable time to true involves a lot of repetition, which can be tedious with 10-20 such scripts defined in the JSON file.

Is it possible to simplfy the JSON file by setting time variable only once in the file and have it apply to all processes?

Also, is it possible to use a template to define error_file and out_file, such as "../logs/${name}-error.log"?

Example pm2.json

{
    "apps": [
      {
        "name": "foo-a",
        "script": "./foo/a.js",
        "args": "-e apple",
        "error_file": "../logs/foo-a-error.log",
        "out_file": "../logs/foo-a-out.log",
        "time": true
      },
      {
        "name": "foo-b",
        "script": "./foo/b.js",
        "args": "-e banana",
        "error_file": "../logs/foo-b-error.log",
        "out_file": "../logs/foo-b-out.log",
        "time": true
      },      {
        "name": "bar-c",
        "script": "./bar/c.js",
        "args": "-e cranberry",
        "error_file": "../logs/bar-c-error.log",
        "out_file": "../logs/bar-c-out.log",
        "time": true
      },

      ...
1 Answers

You can use js file instead json file, it will allow you to create and use common variable/function for repetition part.

const time = true

module.exports = {
    "apps": [
      {
        "name": "foo-a",
        "script": "./foo/a.js",
        "args": "-e apple",
        "error_file": "../logs/foo-a-error.log",
        "out_file": "../logs/foo-a-out.log",
        "time": time
      },
      {
        "name": "foo-b",
        "script": "./foo/b.js",
        "args": "-e banana",
        "error_file": "../logs/foo-a-error.log",
        "out_file": "../logs/foo-a-out.log",
        "time": time
      },      {
        "name": "bar-c",
        "script": "./bar/c.js",
        "args": "-e cranberry",
        "error_file": "../logs/bar-c-error.log",
        "out_file": "../logs/bar-c-out.log",
        "time": time
      },

      ...
Related