1

I'm building a simple staging CI environment where I define the path of the runnable Node application by an environment variable APP_PATH.

Is it possible to tell PM2 in a process config file to look up the startup script by following the path in the APP_PATH variable?

Like this:

{
  "apps": [
    {
      "name": "my_app",
      "script": "$APP_PATH/app.js"
    }
  ]
}

Currently, I get a [PM2][ERROR] script not found : message from PM2 when starting the above configuration.

In a nutshell: PM2 doesn't resolve the env var defined in the script property. Is there any way to overcome this?

bearlysophisticated
  • 543
  • 1
  • 8
  • 19

1 Answers1

3

If you need to do in the same approach. Follow this.

Create a .json file outside your root directory or wherever you need it.

servers.json

[{
  "name":"MyApp",
  "script":"/home/user/app/server.js",
  "instances":"3" //number of instances to start
},
{
  "name":"MySecondApp",
  "script":"/home/user/app2/server.js",
  "instances":"max" //to calculate your number of CPU cores available and run based on the core count
}]

Then run using pm2 start servers.json

This will start the two app with the name and instances mentioned.

Or

With the latest version of PM2 you do not need to write any scripts. Just execute some commands to do the same.

Step 1: First you create your pm2 instances as you need like now many instances you need to run or how many different server you need to run.

Mine is below enter image description here

Once you apps are started and listed like this.

Step 2: Type pm2 startup. Then you will see a auto generated command by pm2 which helps you to create it as a service.

enter image description here

Step 3: You will see the command you need to run in grey shade. Copy that and run it as root user.

once you run that command you will see result like below.

enter image description here

Step 4: The run pm2 save so the present pm2 list of process will be save for startup script.

That's it..

Test it by rebooting your server and check using pm2 ls or pm2 status.

In case you want to update the pm2 process list again the use pm2 update this will take the current process list and update the startup script.

Hope this helps!!!

Prasanth Jaya
  • 4,407
  • 2
  • 23
  • 33