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 overe this?
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 overe this?
- you want to make PM2 to automatically startup your app when reboot happens? – Prasanth Jaya Commented Apr 12, 2017 at 15:44
- @Prasanthchinja yes, that's the plan – bearlysophisticated Commented Apr 13, 2017 at 6:39
1 Answer
Reset to default 4If 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 mands 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
Once you apps are started and listed like this.
Step 2: Type pm2 startup
. Then you will see a auto generated mand by pm2 which helps you to create it as a service.
Step 3: You will see the mand you need to run in grey shade. Copy that and run it as root user.
once you run that mand you will see result like below.
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!!!