Use upstart to encapsulate nodejs applications as system service instances

  • 2020-03-30 03:08:25
  • OfStack

I. introduction of nodejs application common deployment method

Finally, to bring nodejs's application deployment online, copy the source code to the directory using git

/root/deploy/movie

Then do the command:

~ cd /root/deploy/movie
node ./app.js

In the above way, the nodejs program will run in the current console interface, and once the console is finished, the application will stop. Let's change the command so that the program runs in the background


~ node ./app.js &
[1] 21333
[2013-06-21 09:38:30.696] [INFO] console - Start App: http://jb51.net
[2013-06-21 09:38:30.700] [INFO] console - Express server listening on port 3000

So the program starts in the background. The process is working and I don't have to do much.

What if I want to stop the program? Find the nodejs system process and kill it.


~ ps -aux|grep node
root     21333  0.6  3.7 909200 38292 pts/0    Sl   09:38   0:00 node app.js
~ kill -9 21333  

Direct violence. Wouldn't it be nice to be able to start and close nodejs applications like a system service? The next step is to wrap the nodejs application as a system service through upstart.

2. Package the application as an upstart task script


~ vi /etc/init/nodejs-moive.conf
description "node.js jb51.net"
start on startup
stop on shutdown
script
    export HOME="/root/deploy/movie"
    echo $$ > /var/run/moiveme.pid
    export NODE_ENV=production
    exec /usr/bin/node /root/deploy/movie/server.js
    # Log output 
    #exec /usr/bin/node /root/deploy/movie/server.js >> /var/log/moiveme.log 2>&1
end script
pre-start script
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/moiveme.log
end script
pre-stop script
    rm /var/run/moiveme.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/moiveme.log
end script

Use upstart to manage nodejs applications

Launch the nodejs-moive application (the task script above) with process ID: 21257


~ start nodejs-moive
nodejs-moive start/running, process 21257
~ tail -f /var/log/moiveme.log
[2013-06-21T09:21:17.122Z] (moive.me) Starting
~ ps aux|grep node
root     21257  8.0  3.7 909204 37824 ?        Ssl  09:21   0:00 /usr/bin/node /root/deploy/movie/server.js

Check the running status, process 21257 is running normally

~ status nodejs-moive
nodejs-moive start/running, process 21257

Kill nodejs application process 21257, through upstart management, nodejs-moive application will automatically restart


~ kill -9 21257
# Automatic restart log 
~ tail -f /var/log/moiveme.log
[2013-06-21T09:21:33.662Z] (moive.me) Starting
# View the system process and find the progress ID changed 
~ ps -aux|grep node
root     21280  9.1  3.7 909204 37704 ?        Ssl  09:21   0:00 /usr/bin/node /root/deploy/movie/server.js
# View process status, process ID It did change, and it was done automatically 
~ status nodejs-moive
nodejs-moive start/running, process 21280

This makes it easy to manage nodejs applications as a system service through upstart. Operations will be easy!!


Related articles: