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.
Related
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.
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.
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
I'm using a library, say I want the script that starts my app to be cli-lib start how can i go about that? I don't want to just run node app.js or any js file for that matter, the cli lib does that for me but i cant figure out a way to get this to work.
Yes you can run any process type with pm2.
For scripts in other languages which has assigned an interpreter by default,
pm2 start echo.coffee
pm2 start echo.php
pm2 start echo.py
pm2 start echo.sh
pm2 start echo.rb
or with interpreter
pm2 start echo.pl --interpreter=perl
read more from docs
I think you can create a bash script then run it with pm2, for examples:
bash.sh
#!/usr/bin/bash
node /home/user/test.js
then you can run file "bash.sh" with pm2
pm2 start bash.sh
I did not test it yet but you can try.
Yes we can use & operate pm2 related command with script in Linux.
create script file for run.
$ sudo nano runpm2.sh
add your relevant command for run pm2. Also you can visit for more command on this site
#!/usr/bin/bash
pm2 restart "a"
pm2 restart "b"
pm2 restart "c"
pm2 start "app.js"
pm2 restart "app_name"
pm2 reload "app_name"
pm2 stop "app_name"
pm2 delete "app_name"
Assign access for run script.
sudo chmod +x runpm2.sh
run .sh file with this command.
sudo ./runpm2.sh
in an Ubuntu Server, I am unable to run the node.js app in cluster mode using PM2.
The command I use is :
PM2 start server.js --name Server -i max
When I list the PM2 processes, I can see the Server has Error status.
I have tried looking into the log file generated by PM2 but it's empty.
I am however able to run the same server.js without the cluster mode using :
PM2 start server.js --name Server
doing PM2 Kill and starting all the services again was the solution to above issue.
You could also have used pm2 restart Server to restart it
If you use pm2 kill you will just kill all processes, to clean up afterwards i would recommend to use pm2 flush so all logfiles will be reset
I have gone through this same kind of situations but in my case pm2 is showing error status cause of error in my code.
use the below command
pm2 logs
pm2 logs command helped me by showing some hints to check where exactly the error is occured.
if everything works fine then pm2 list will show you the status online.
you can check the ports running by pm2(not only pm2 but all the process) using below command
sudo netstat -tulpn