Use upstart to encapsulate NODEJS applications as system service instances _node.js

Source: Internet
Author: User
Tags stop script

Introduction of NODEJS Application of general deployment method

Finally, we're going to deploy the Nodejs application and copy the source code from git to the directory.

Copy Code code as follows:
/root/deploy/movie

And then make an order:
Copy Code code as follows:

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

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

Copy Code code as follows:

~ 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

This allows the program to start in the background. The process is working, and I don't have to do much about it.

What if I want to stop this program? Find the NODEJS system process and kill again.

Copy Code code as follows:

~ 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 and violent settlement. If you can start and turn off Nodejs applications like system services, how nice! The following is accomplished by upstart to encapsulate the Nodejs application as a system service.

Second, the application is encapsulated as a upstart task script

Copy Code code 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

#日志输出
#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

Third, the use of upstart management Nodejs application

Start the Nodejs-moive application (the task script above), the process id:21257

Copy Code code as follows:

~ 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


View run status, process 21257 is working
Copy Code code as follows:

~ Status Nodejs-moive
Nodejs-moive start/running, Process 21257

Kill Nodejs Application Process 21257, through upstart management, nodejs-moive application will automatically restart

Copy Code code as follows:

~ kill-9 21257

#自动重启日志
~ Tail-f/var/log/moiveme.log
[2013-06-21T09:21:33.662Z] (moive.me) starting

#查看系统进程, the Discovery 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

#查看进程状态, the process ID does change and is automatically completed
~ Status Nodejs-moive
Nodejs-moive start/running, Process 21280


It is convenient for us to manage NODEJS applications in a system-service manner through upstart. It will be very easy to carry the dimension!!

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.