πŸ“œ ⬆️ ⬇️

Autostarting Node.js application on CentOS 6.2

There are many ways to autorize Node.js applications, but after some searches I managed to find a solution that works and does not present much difficulty.

At first I tried forever - it works perfectly, but only until you need to start the application when the system boots. I tried to add a line to /etc/rc.d/rc.local - sometimes it worked, and sometimes it didn't, and so far I haven’t figured out why.

Then I looked at Upstart and at Monit . Upstart made writing a script for the autoloader as easy as autoexec.bat during DOS, and Monit can check the application to make sure it works all the time.
')
Then I realized that my need is simple: first, start the application when the system boots, and second, restart if it crashes (with the Node.js applications this happens) - and one upstart is enough to arrange and both.

Upstart is already included in CentOS 6.2, so all you have to do is put the text file in / etc / init - calling it, for example, myapp.conf:

#!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 on CentOS 6.2, you can do with this to start the application and to keep it running. I also found that running Centle through runlevel [2345] is necessary on CentOS, and other options do not seem to work.


Translator's Notes:

Source: https://habr.com/ru/post/243429/


All Articles