pm2 starts two apps with same name and other versions - node.js

When I am executing this script it starts two apps with same name and other versions
pm2 start --name rpc --log RPC_LOG_FILE node dist/main.js
logs are same, but when I start with ecosystem config file it starts 1 app.
I don’t want to use config file, this is the first time I met this issue, is there a way to fix this or is this normal and what is the purpose ?

Start pm2 by process_id.
In your case it is 3 or 4
pm2 start <process_id> --log RPC_LOG_FILE node dist/main.js
You can rename your process names
pm2 restart id|name -n newname
or
pm2 delete id|name
pm2 start app.js -n newname

:D problem is that I was running also node command node dist/main.js which is wrong, must be dist/main.js

Related

How to migrate pm2 processes from one server to another?

I am using pm2 for managing node processes on one of the servers.
The package is here: https://pm2.keymetrics.io/
It is open source and available both on npmjs and GitHub.
I can easily install it every time using: npm i pm2 -g
I love pm2, and not just node processes, I write bash scripts and run them as cron under pm2 and I can easily check the logs.
Some commands:
pm2 --name "process-name" start "bash script.sh"
pm2 --name "node-process" start "node main.js"
pm2 logs node-process
pm2 stop node-process
pm2 restart node-process
There are 2 more commands which are very useful to start pm2 on startup with all the processes automatically.
pm2 startup Will generate startup script.
pm2 save Will update start script with current processes.
Everything is good. But, today I got into a problem.
I am running all pm2 node processes from a folder /mnt/node.
What I want is that I have synced that /mnt/node folder to another server and I am trying to find a way to move all pm2 processes automatically to another server without writing each process once again.
May be someone can help.
You can do this.
On the source server:
pm2 save
copy file saved on ~/.pm2/dump.pm2 to destination server, then:
pm2 resurrect
Haven't try this between two differents server yet but i think it will be ok.

Nestjs with pm2, port still be used after killing the process with pm2 stop?

I deployed my Nest.js application on my VPS (Ubuntu 20.04 DO) using pm2 as process manager. Here is my step by step when I'm updating the app.
$ pm2 stop 1
$ kill -9 $(sudo lsof -t -i:4040)
$ npm run build
$ pm2 start 1
Notice that I must kill the port that the app use before I proceed to the build one, how to simplify it and become like these :
$ npm run build
$ pm2 reload 1
So I can deploy my Nest.js app gracefully and achieve at least only 1% downtime
Stop command keeps the app in the apps list while the delete command not.
I think you want something like
start.sh
#!/bin/bash
source .env
ENVIRONMENT="$NODE_ENV"
npm run build || exit
pm2 delete "$ENVIRONMENT"
pm2 start "npm run start:prod" --name "$ENVIRONMENT" --log-date-format 'DD-MM HH:mm:ss.SSS'
If you are not using different environments, an equivalent script would be
#!/bin/bash
npm run build || exit
pm2 delete my_application
pm2 start "npm run start:prod" --name my_application --log-date-format 'DD-MM HH:mm:ss.SSS'
Emitting the --log-date-format is perfectly fine. However, I have included it because I suspect it might become helpful when finding bugs in production down the line.

How to run 2 separate apps in PM2?

Right now I am using pm2 to run a node server app. I do that with pm2 start npm. This seems to be independent of the current directory.
I found some mentions online to use pm2 start npm --name "app_name" -- start. However, no matter what name I specify and directory I am inside, it always starts the same app.
Due to the nature of node, I don't run a single .js file and just type npm start in the current directory.
Edit: From my understanding, the problem seems to be that pm2 always starts /usr/bin/npm (Starting /usr/bin/npm in fork_mode (1 instance). So the --name flag doesn't matter much, ie. I can get a list of the same app with different names, and this app is node app A and sometimes node app B. I am kinda lost
What is happening is you have a PM2 app named npm, thus the confusion. You can list pm2 apps with pm2 ls
First, remove it using :
pm2 del npm
Then, start a new app, naming it :
pm2 start npm --name "app_name" -- start
Then, the second app (in the other directory) with :
pm2 start npm --name "app_name2" -- start
You can run Multiple apps using PM2 just follow below steps:
Firstly enter into directory A and start it on PM2 pm2 start server.js --name app-name
Save this in PM2 using pm2 save
Now to run other app you need enter into directory B and start app using
pm2 start server.js --name app-name
Save this process as well, and now check PM2 list using pm2 ls

Starting node app whenever it stops

I don't have any errors in my node app, but it stops after some requests. Though i have enabled the logs and checked, its showing on different requests.
Is it possible to start the node app whenever it is stopped ?
Currently i am using this to start the node app
To start the app
nohup npm start &
And to find the process id
ps aux | grep node
And to kill certain process
kill -9 PID
I don't want to leave the app stopped. It should be ever running. So, Is it possible to start the app whenever it gets stopped.
Thanks
You can use PM2 to monitor and restart your app for you.
By default if you run your app with PM2 it will auto-restart it if it crashes.
npm install pm2#latest -g
pm2 start index.js
PM2 can also be used to run multiple instances and has commands to stop/restart individual ones.
You can get the process PID using pm2 list as well.
More robust use case:
Start up your app by having PM2 run 'npm start' and also give it a name to refer to it later (it will auto restart if it exits/crashes)
pm2 start npm --name="mySuperApp" -- start
Later you make some changes and want to restart it:
pm2 restart mySuperApp
Eventually you want to stop the app:
pm2 stop mySuperApp
you can use nodemon
installing it with npm install -g nodemon
and running your code like this : nodemon index.js
Finding more documentation here
It will run until your app encounter an error but you can restart it with rs in your console

pm2 - How to start if not started, kill and start if started

I am trying to start pm2 if it is not running, or kill it and start if it is, how can I achieve this behavior in the WINDOWS command line interface?
There are plenty of solutions using grep in linux but nothing for windows, any idea on how to get this behaviour?
The documentation says that pm2 start -f app.js will kill and start the app but it actually just creates another instance.
Use this:
pm2 delete main.js 2> /dev/null && pm2 start main.js
This part: 2> /dev/null - will simply redirect the stderr to the /dev/null, meaning to nowhere.
It does not seem there is a "single command" way to do this, which is rather important in many development environments, so here are some options:
put soyuka's suggestion on one line.
pm2 stop myprocess; pm2 start myprocess.js
This will output errors, but it will work.
They also have this option built into their ecosystem tools. To use this, go into the folder you are working with and run
pm2 ecosystem
This will generate a file ecosystem.config.js which you will need to make sure your name and script are correct within.
You can then use the command:
pm2 startOrReload ecosystem.config.js
I, however also want to see my logging, so I use this command:
pm2 flush && pm2 startOrReload ecosystem.config.js && pm2 log
This will also flush the logs so you are not seeing old logs.
You can do something like this
pm2 delete your_app_name || : && pm2 start index.js -i 1 --name 'your_app_name'
The : is a null operator that returns 0 success exit code. So whatever happens, pm2 start command will execute (even if pm2 delete fails, for the case where the app does not exist yet).
I'd do this :
pm2 stop myprocess.js #this will just say process not found
pm2 start myprocess.js
Or if you want to clear everything :
pm2 kill
pm2 stop
If you want more advanced possibilities check out the pm2 api.

Resources