When I restart the ubuntu server the node.js services in the server are not running in the background.
I need to connect to the server and start the services whenever I restart the server.
You need to use pm2 is a advanced process manager for production Node.js applications. Load balancer, logs facility, startup script, micro service management, at a glance.
https://pm2.keymetrics.io/
With this you can run in background
Use PM2.
To install PM2:
npm install -g pm2
To start a node.js process:
pm2 start -n "My task name" /path/to/node/script
To list process:
pm2 list
To restart manually a process (after update for example):
pm2 restart <id of process>
PM2 will handle restart automatically in some case, or you can also tell PM2 to restart node process if script change. And many other cool things.
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.
In my centos server, I have an API with node js and I started with pm2. pm2 status is online and It works correctly. but when server reboot, can't connect to the API although pm2 status is online.
I also try start node js like below
pm2 start index.js
pm2 startup
pm2 save
but It does not work until I restart pm2 ..
how can I fix it?
You have to do pm2 resurrect to start an application after "pm2 saving" it:
pm2 resurrect
Then put this command in a script file and configure it to run on server reboot. Read this Q and A for help. Also in this article there are some CentOS specific changes that need to be done.
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.
I am trying to use the module pm2 to start my node js app everytime the server boots.
I have used the command pm2 startup ubuntu but each time I restart the server, my application is not running and I have to start it manually again.
Any ideas what is causing this issue?
Make sure you do save your processes:
pm2 start app.js
pm2 startup ubuntu
pm2 save
Once you have started the apps and want to keep them on server reboot
do: pm2 save
Source: https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#startup-script
Go to your server directory and use the following commands:
pm2 start <your_app_name.js>
pm2 startup ubuntu
pm2 save