Autostart Node.js application on CentOS 6.2

Original author: Strictly Programming
  • 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 tried forever - 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 Upstart and Monit . Upstart makes writing a script for the autoloader as simple as autoexec.batin 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 file in / etc / init - name it, for example, myapp.conf:

description "testjs.js "
author      "sample"
start on runlevel [2345]
stop on shutdown
    export HOME="/root"
    echo $$ > /var/run/
    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/
    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 runlevel [2345] is necessary, but other options do not seem to work.

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 ”, stopand “ 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, the Express.js server with one command (and not two) , in order to clear some 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/ " and without the opposite command " rm /var/run/ ", 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 “ respawnhint 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 a web server or some other server.

    Here is a simplified (but working) example for  Express.js :

    app.get('/тайный_адрес_перезагрузчика', function(req, res){
       res.send('The Express.js web server will shut down (and restart) in a second.');
       console.log('Remote shutdown.');
       }, 1000);

Also popular now: