Use upstart to encapsulate nodejs applications as system service instances

Source: Internet
Author: User
Tags stop script

I. Introduction to General Deployment of nodejs applications

Finally, we need to deploy the nodejs application online and copy the source code to the directory through git.
Copy codeThe Code is as follows:/root/deploy/movie
Run the following command:
Copy codeThe Code is as follows:
~ Cd/root/deploy/movie
Node./app. js
In the preceding method, the nodejs program runs on the current console interface. Once the console ends, the application stops. Let's change the command to run the program in the background.

Copy codeThe Code is as follows:
~ Node./app. js &
[1] 21333
[09:38:30. 696] [INFO] console-Start App: http://jb51.net
[09:38:30. 700] [INFO] console-Express server listening on port 3000
In this way, the program is started in the background. The process is running normally and I don't need to do too many things.

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

Copy codeThe Code is as follows:
~ Ps-aux | grep node
Root 21333 0.6 3.7 909200 38292 pts/0 Sl node app. js
~ Kill-9 21333
Directly solve the problem. How nice is it to start and close nodejs applications like system services! The following describes how to encapsulate nodejs applications as system services through upstart.

2. encapsulate the application as an upstart task script

Copy codeThe Code is as follows:
~ 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

3. Use upstart to manage nodejs applications

Start the nodejs-moive application (the preceding task script), process ID: 21257

Copy codeThe Code is as follows:
~ Start nodejs-moive
Node. js-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/usr/bin/node/root/deploy/movie/server. js
Check the running status. Process 21257 is running normally.
Copy codeThe Code is as follows:
~ Status nodejs-moive
Node. js-moive start/running, process 21257
Kill the nodejs application process 21257 and use upstart management to automatically restart the nodejs-moive application.

Copy codeThe Code is as follows:
~ Kill-9 21257

# Automatic Restart log
~ Tail-f/var/log/moiveme. log
[2013-06-21T09: 21: 33.662Z] (moive. me) Starting

# Check the system process and find that the ID has changed
~ Ps-aux | grep node
Root 21280 9.1 3.7 909204 37704? Ssl/usr/bin/node/root/deploy/movie/server. js

# Check the Process status. The process ID changes and is automatically completed.
~ Status nodejs-moive
Node. js-moive start/running, process 21280
In this way, we can easily use upstart to manage nodejs applications in the form of system services. It will be easy to maintain !!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.