Create a basic Web site using Nodejs microblog--node. JS Development Guide 3

Source: Internet
Author: User

    1. Using the cluster module

Create Cluster.js, call App.js

Var cluster = require (' cluster '); Var os = require (' OS ');//Get the number of CPUs var  Numcpus = os.cpus (). Length;var workers = {};if (Cluster.ismaster)  {  // Main process Branch   cluster.on (' Exit ', function  (worker)  {    //when a worker process finishes, Restart the work process     delete workers[worker.pid];    worker =  Cluster.fork ();     workers[worker.pid] = worker;  });   // Initial open worker process with same number of CPUs   for (var i = 0; i < numcpus; i++)  {     var worker = cluster.fork ();     workers[worker.pid]  = worker;  }} else {  //worker Process Branch, starting server   var app =  require ('./app ');   app.listen (3000);} When the main process is terminated, close all worker processes Process.on (' SIGTERM ',  function ()  {  for (&NBSP;VAR&Nbsp;pid in workers)  {    process.kill =  (PID);  }   process.exit (0);});

Run at Terminal

$node Cluster.js

You can see that multiple node processes (4-core CPUs) are started in the process list.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/45/3E/wKioL1PkvxiAEYmLAAObnKBzTZA089.jpg "title="    2014-08-08 19:50:13 screen. png "alt=" wkiol1pkvxiaeymlaaobnkbztza089.jpg "/> Cluster.js creates a server process with the same number of CPU cores to ensure that CPU resources are fully utilized. A worker process initiated through cluster can directly implement port multiplexing, so all worker processes only need to listen to the same port.

2. Start the script

Create a Mynodeapp file

#! /bin/shnode_env=productiondaemon= "Node /home/nick/nodejs/examples/express_example/cluster.js" NAME =mynodeappdesc=mynodeapppidfile= "Mynodeapp.pid" case  "$"  in  start)          echo  "starting  $DESC: "                  nohup  $DAEMON  > /dev/null &         echo $1 >  $PIDFILE          echo  "$NAME."                 ;;    stop)         echo  "stopping  $DESC: "                 pid= ' cat  $PIDFILE '         kill  $pid                 rm  $PIDFILE          echo  "$NAME."                 ;; Esacexit 0

$chmode +x Mynodeapp

Give execute permission to start the server through Nohup, so that the process does not shut down because it exits the terminal, while the main process PID is written to the file, when the call ends, the PID is read from the file, and the main process is terminated to shut down the server.

The result is not good, you can start the program, but you cannot close it.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/45/3E/wKiom1PkxbuCYFghAAPDsGy0I8I375.jpg "style=" float: none; "title=" 2014-08-08 20:34:56 screen. png "alt=" wkiom1pkxbucyfghaapdsgy0i8i375.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/45/3E/wKioL1PkxtTz3s-0AAOH1nVlYD8908.jpg "style=" float: none; "title=" 2014-08-08 20:41:05 screen. png "alt=" wkiol1pkxttz3s-0aaoh1nvlyd8908.jpg "/>

How to create a boot script from Ubuntu

Auto Number:

$ cd/etc/init.d$ sudo chmod 755/etc/init.d/filename$ sudo update-rc.d filename defaults

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/45/3E/wKiom1PkyNPS06AZAAPB7tgGZ60439.jpg "title=" 2014-08-08 20:55:07 screen. png "alt=" wkiom1pkynps06azaapb7tggz60439.jpg "/>

Specify number:

$ cd/etc/init.d$ sudo chmod 755/etc/init.d/filename$ sudo update-rc.d filename defaults N

The number must be between 0 and 99.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/45/3E/wKiom1PkxxSg1LLNAATn9H7jhVE823.jpg "title=" 2014-08-08 20:45:51 screen. png "alt=" wkiom1pkxxsg1llnaatn9h7jhve823.jpg "/>

If you want to uninstall the startup script:

$ cd/etc/init.d$ sudo update-rc.d-f filename remove

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/45/3F/wKioL1PkyJnDsLVvAATn9H7jhVE232.jpg "title=" 2014-08-08 20:45:51 screen. png "alt=" wkiol1pkyjndslvvaatn9h7jhve232.jpg "/>


This article is from "Whispering Autumn Wind" blog, please be sure to keep this source http://xjhznick.blog.51cto.com/3608584/1537612

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.