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.
Related
I want to set up ApplicationStart hook for node.js project where pm2 is used as a process manager in aws ec2 server.
I checked some tutorials and the shell script for ApplicationStart hook contains commands for running the project by using:
node/npm
pm2
for example in this tutorial, the shell script contains:
npm start
pm2 start npm --name "covidapp" -- start
in this tutorial, the shell script contains:
pm2 start npm --name "myApp"
node app.js > app.out.log 2> app.err.log < /dev/null &
Why we are running the project two times? Why we just don't use pm2?
I've deployed several apps on EC2 using PM2 and in my experience there should be no need (or benefit) to use node app.js, npm start or similar.
As you are probably already guessing, the whole point of PM2 is to run the process(es).
My recommendation would be to create a PM2 ecosystem configuration with all needed configurations, number of processes, ENV vars etc. I personally prefer this way even when running only one single node application on the server.
https://pm2.keymetrics.io/docs/usage/application-declaration/
And start the process(es) using the configuration, eg.:
pm2 start ecosystem.config.js
I also recommend using PM2 startup generator to make sure PM2 is started on server reboot: pm2 startup
https://pm2.keymetrics.io/docs/usage/startup/
Once you have the startup script generated. Start your processes using pm2 manually or by using a configuration file (see example above). Verify with pm2 status that all processes are running as expected and execute pm2 save to "snapshot" the current state. The saved state will now automatically respawn on reboot.
hello I am total beginner when it comes to nodejs.
I was working on PHP code but found this app that will suit my needs for a project this is my first time to nodejs and managed to get this app working. https://github.com/KiraLT/torrent-stream-server .
the command to get it running is torrent-stream-server serve
which works great but i have to keep it running and not touch for it to work any help with this please?
i have spent my whole night searching i found like forever and pm2 but i did not know how to use since serve command
go to the folder path of project
npm i
npm install pm2#latest -g
pm2 start app_name if you want to run attached project to the question you can use this command: pm2 start npm --name "app_name" -- start
pm2 startup
pm2 save
note : pm2 startup can generate startup scripts and configure them in order to keep your process list intact across expected or unexpected machine restarts.
note : pm2 save to freeze a process list for automatic respawn
for managing application state is simple here are the commands :
pm2 ls
pm2 restart app_name
pm2 reload app_name
pm2 stop app_name
pm2 delete app_name
pm2 logs app_name
How can I run multiple nodejs applications using command npm start in different directories using pm2 on boot? Also how do i setup them to start again if any error occurs?
I figured out how to start npm start with pm2 using - "pm2 start npm -- start"
but I don't understand how to set up them to boot.
Based on my research "pm2 startup" gives the command to setup startup which I passed as it is in the terminal.
How do I set up the rest?
Thanks,
If you've already got the part where pm2 automatically starts when you reboot, that's good.
You then need to add each of your apps to pm2.
Go into each of your project directories and run
pm2 start your_file.js --name=my_app, where your_file.js is the file you would use to start with regular node. (ie: node server.js, or node index.js, etc.) and my_app is a friendly name that you will see with pm2 status and can use to apply future commands to.
This should start your app. If you run pm2 status it is hopefully in the "running" state.
To save it so that it automatically gets booted when you reboot or restart pm2 you need to run pm2 save whenever you change app configurations.
You can do this for as many apps as you need to start.
I have tried so many ways to keeping node server alive on Linux environment but nothing has worked. Sometime the server runs only 4-5 hours and sometime it runs 10-12 hours and after that server goes shut down automatically.
I have tried forever start, pm2, nodemon but nothing has worked.
I have also tried shell script with forever start for running it but that also not worked.
Applications that are running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). Luckily, PM2 provides an easy way to do this, the startup subcommand.
The startup subcommand generates and configures a startup script to launch PM2 and its managed processes on server boots:
$ pm2 startup systemd
Run the command that was generated (similar to the highlighted output above, but with your username instead of sammy) to set PM2 up to start on boot (use the command from your own output):
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy
check here for details https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
You can increase the size of memory restar- check this: pm2 process crashed on server. it gives an error
Try using process manager for making the application run all the time. Here is the link for Pm2 . It will restart you application once it crashes also automatically
Use a NPM package called nodemon
npm install -g nodemon
nodemon index.js
If the application fails or crashes for any reason it will restart
Read more at
https://www.npmjs.com/package/nodemon
I have my app running on http://talkwithstranger.com/ and I have deployed it on AWS EC2. I use this command
sudo nohup node index.js &
To continue running my Node JS server even if I close my terminal and exit my SSH.
However, after 2 days everytime I wake up and I find out that the node server itself stops automatically. I checked the running processes by using
ps -ef
and my node script is not there.
Google Chrome say site DNS not found, because NodeJS Express is not running of course to serve my html file, but why it stops itself?
What is causing this unexpected shutdown of my server after every 2 days? I have to manually run nohup again to run it again.
Does nohup has a time to expire or something ?
You should run node.js using a service / process manager. You can use something basic such as forever or supervisord but I would actually advise you to take a look at PM2.
It can do a lot of things - one of them being that it manages your process, makes sure it keeps running, restarts it when it fails, manages the logs, etc. You can also have it autostart when you restart the server.
It becomes really powerful in combination with https://pm2.io, because this enables you to monitor your server's metrics such as CPU and memory remotely and see whether exceptions happened, and much more (such as even remotely updating the software by pulling from git). However, they no longer offer a free plan unfortunately - their plans now start at $79/month, which is a pity. But don't worry, the PM2 application itself is still free and open source, only the monitoring costs money.
Basic usage of PM2:
npm install -g pm2
...to install PM2.
pm2 start my_script.js
Starts a script and lets it run in background.
pm2 status
Shows the status of any running scripts.
pm2 restart all
Restarts all running scripts.
pm2 kill
Stops all scripts and completely shuts down the PM2 daemon.
pm2 monit
Monitors CPU/RAM usage and shows it.
pm2 logs
Shows the last 20 output and error log lines and starts streaming live logs to the console. The logs are stored in the folder ~/.pm2/logs.
Using PM2, your script will not stop - at most, it will restart. And if it does you will be able to more easily understand why because you can easily access logs and watch what happenes with memory usage, etc.
Extra tips:
To avoid filling up the harddisk with logfiles, I recommend installing the module pm2-logrotate:
pm2 install pm2-logrotate
To automatically launch PM2 with the same script on startup when the server starts, you can first save the current configuration:
pm2 save
...and then use the following command to install a startup script - follow the instructions displayed, which will be different based on the exact OS you are using:
pm2 startup
To use PM2 in a more advanced way with multiple processes, custom environment variables, etc., take a look at ecosystem files.
You can try forever.Install using the following command.
npm install -g forever
Then just start forever:
forever start index.js
Another better option for production use is pm2.You can install pm2 with below command
npm install -g pm2
# or
yarn global add pm2
start server
pm2 start index.js
The best thing is you can achieve load balancing with pm2(utilize all available CPU)
pm2 start index.js -i max
For more info, you can visit pm2 documentation page.