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?
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.
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:
to create the service file
/etc/systemd/system/nodeserver.service
.Enable the service by
systemctl enable nodeserver.service
.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
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:
- [no] Do not automatically restart the container when it exits. This is the default
- [failure] Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts.
- [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.
(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
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.
© 2022 - 2024 — McMap. All rights reserved.