How to auto-restart node app?

孤者浪人 提交于 2020-05-26 06:14:53

问题


I am running a node strongloop application and need for it be auto-restarted if it crashes. what is the best way to auto restart a node application? Also is there anyway to be notified when these crashes occur?


回答1:


You could use process managers like Strongloop process manager (http://strong-pm.io/), PM2 (http://pm2.keymetrics.io/) and Forever (https://github.com/foreverjs/forever). Also check the comparison of these three (http://strong-pm.io/compare/). You can also check pm2-monitor (https://www.npmjs.com/package/pm2-monitor) for notifications along with server restart.




回答2:


Most Linux systems have recently switched to using systemd, which makes this process a lot simpler and more efficient, and means that we do not need forever any more.

All you need is:

  1. to create the service file /etc/systemd/system/nodeserver.service.

  2. Enable the service by systemctl enable nodeserver.service.

  3. Start the service by systemctl start nodeserver.service.

You always can check its availability by running following command: systemctl status nodeserver.service.

If you make any changes to the service file, you will need to do a systemctl daemon-reload before reloading the service systemctl restart nodeserver.service.

An example of .service file:

[Unit]
Description=Node.js Example Server
#Requires=After=mysql.service       # Requires the mysql service to run first

[Service]
ExecStart=/usr/local/bin/node /opt/nodeserver/server.js
Restart=always
RestartSec=10                       # Restart service after 10 seconds if node service crashes
StandardOutput=syslog               # Output to syslog
StandardError=syslog                # Output to syslog
SyslogIdentifier=nodejs-example
#User=<alternate user>
#Group=<alternate group>
Environment=NODE_ENV=production PORT=1337

[Install]
WantedBy=multi-user.target

By the way there is an official article from StrongLoop about best practices and systemd is mentioned. Read more.

Also you might want to look at this module built for nodejs and strongloop by strongloop guys: https://github.com/strongloop/strong-service-systemd




回答3:


PM2 and forever are good ways to manage your node process. PM2 is to provide a friendly graphical interface so that you can get the CPU/Memory/logs easily.

Here I offer another interesting idea : Docker.

You can build your runtime environment using docker image from docker hub

And run it:

docker run -it [node images] node [your script.js]

You can manage the lifecycle of your process using the restart arguments.

Docker supports the following restart policies:

  1. [no] Do not automatically restart the container when it exits. This is the default
  2. [failure] Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts.
  3. [always] Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely.The container will also always start on daemon startup, regardless of the current state of the container.



回答4:


(Assuming your app is running on linux) You could always write a service to ensure that the app is running, and restart if necessary. Here's a how to on writing a service:

http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html




回答5:


please, have a look at this article: https://strongloop.com/strongblog/comparison-tools-to-automate-restarting-node-js-server-after-code-changes-forever-nodemon-nodesupervisor-nodedev/




回答6:


I'm using init.d to keep it starts every time server reboot, you can read this page, http://www.slidequest.com/q/70ang, it covers almost everything, or Ghost has really good example here https://github.com/TryGhost/Ghost-Config/blob/master/init.d/ghost

Then you register and enable it by update-rc.d yourapp defaults and control it via start, stop etc.

I'm using monit to watching stuff, and have not meet any trouble so I can't say is it good or not. You can read it at digitalocean.



来源:https://stackoverflow.com/questions/36417528/how-to-auto-restart-node-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!