
Autostart Node.js application on CentOS 6.2
- Transfer
- Tutorial
There are many ways to autostart Node.js applications, but after some searching, I managed to find a solution that works and does not present much difficulty.
At first I triedforever - it works fine, but only until you need to run the application at boot time. I tried adding a line to /etc/rc.d/rc.local - sometimes it worked, and sometimes not, and so far I have not figured out why.
Then I looked at Upstartand Monit . Upstart makes writing a script for the autoloader as simple as autoexec.bat in the days of DOS, and Monit can check the application to make sure that it works all the time.
Then I realized that my need is simple:firstly, launch the application at the initial boot of the system, and secondly, restart if it crashes (this happens with Node.js applications ) - and one upstart is enough to arrange and one and the other.
CentOS 6.2 already includes upstart, so you just have to put the text filein / etc / init - name it, for example, myapp.conf:
After that it will be possible to give such commands:
The application will also be launched at boot time, and in CentOS 6.2 you can do just this to launch the application and to keep it running. I also found that running CentOS onrunlevel [2345] is necessary, but other options do not seem to work.
Translator Notes:
At first I tried
Then I looked at Upstart
Then I realized that my need is simple:
CentOS 6.2 already includes upstart, so you just have to put the text file
#!upstart
description "testjs.js "
author "sample"
start on runlevel [2345]
stop on shutdown
respawn
script
export HOME="/root"
echo $$ > /var/run/testjs.pid
exec /usr/local/bin/node /root/test.js >> /var/log/testjs.log 2>&1
end script
pre-start script
# Формат даты тот же, что и у (new Date()).toISOString(), а не отсебятина
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/testjs.log
end script
pre-stop script
rm /var/run/testjs.pid
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/testjs.log
end script
After that it will be possible to give such commands:
/sbin/start myapp
/sbin/stop myapp
/sbin/status myapp
The application will also be launched at boot time, and in CentOS 6.2 you can do just this to launch the application and to keep it running. I also found that running CentOS on
Translator Notes:
- Upstart does not need the description
and author fields, so you can do without them in the script instead of filling it with all sorts of nonsense.
- If the paths in the system are correctly registered, then in the script you can simply type
“ node ” instead of“/ usr / local / bin / node”; the same applies to the“ start ”, “ stop ” and “ status ” commands, which then will not need the prefix“/ sbin /”.
- In addition to the above, there is also a
“ restart ” command, which starts first“ stop ”, and then“ start ”. It is useful in cases when you have to restart, for example, theExpress.js server with one command (and not two), in order to clearsome kind of cache that the server has in memory, or to force the server to be guided by the new version of the javascript file, describing the routing on the site.
- Team
« start » can be served without fear: she refuses to work if the application with the specified name uzhé she started, and will not trigger an unnecessary second copy of it.
- Without the line
" echo $$> /var/run/testjs.pid " and without the opposite command" rm /var/run/testjs.pid ", as a rule, you can do it. You won’t have to kill a process by its number: it’s much easier to give the“ stop ” (or “ restart ”) command, which uses the name of the application, which is always the same, and it is better remembered.
- The “ respawn ” hint works so well in upstart that all the usual exit methods from the javascript application (for example,
“ process.exit (0) ”) also cause it to restart, and not to permanently stop it. You can use this if you need to provide an interface for remotely restarting aweb server or some other server.
Here is a simplified (but working) example for Express.js :app.get('/тайный_адрес_перезагрузчика', function(req, res){ res.type('text/plain'); res.send('The Express.js web server will shut down (and restart) in a second.'); console.log('Remote shutdown.'); setTimeout(function(){ process.exit(0); }, 1000); });