Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

node.js - Amazon EC2 NodeJS server stops itself after 2 days even after using sudo nohup

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...