Use PM2 to manage nodejs processes and pm2nodejs Processes

Source: Internet
Author: User
Tags node server

Use PM2 to manage nodejs processes and pm2nodejs Processes

Pm2 is a process manager for a Node application with the load balancing function.

When you want to use your independent code to take advantage of all the CPUs on all servers, and ensure that the process is always alive, 0 seconds of heavy load, PM2 is perfect.

It is very suitable for the IaaS structure, but does not apply it to the PaaS solution (the Paas solution will be developed later ).

Compared with using node index. js, it has the following advantages:

1. You can manage multiple node server processes in a command window. Multiple processes of the node Command need to open multiple windows.

2. Close the command window and the node process will still run. After the node command is run to close the window, the process is closed.

1. Install PM2

npm install -g pm2

2. Start index. js with pm2

Pm2 start index. js -- name tank

-- Name tank is to give this process a name

Other pm2 commands

Install npm install-g pm2 usage $ npm install pm2-g # install pm2 $ pm2 start app on the command line. js-I 4 # Run pm2 in the background and start four apps. js # You can also pass the 'Max' parameter to start # the correct number of processes depends on the number of Cpu cores $ pm2 start app. js -- name my-api # name Process $ pm2 list # display all process statuses $ pm2 monit # monitor all processes $ pm2 logs # display all process logs $ pm2 stop all # stop all processes $ pm2 restart all # restart all processes $ pm2 reload all #0 s stop the heavy-load process (for NETWORKED processes) $ pm2 stop 0 # stop a specified process $ pm2 restart 0 # restart a specified process $ pm2 startup # Generate an init script to keep the process alive $ pm2 web # Run a robust computer API endpoint (http: // localhost: 9615) $ pm2 delete 0 # kill a specified process $ pm2 delete all # different methods to kill all processes running: $ pm2 start app. js-I max # start the maximum number of processes based on the number of valid CPUs $ pm2 start app. js-I 3 # start three processes $ pm2 start app. js-x # Start the app in fork mode. instead of using cluster $ pm2 start app. js-x ---a 23 # Start the app in fork mode. and pass the parameter (-a 23) $ pm2 start app. js -- name serverone # start a process and name it serverone $ pm2 s Top serverone # Stop the serverone process $ pm2 start app. json # start the process in the app. set the option $ pm2 start app in json. js-I max ---a 23 # post -- to the app. the parameter $ pm2 start app is passed in js. js-I max-e err. log-o out. log # Start and generate a configuration file. You can also execute an app (fork mode) written in other languages ): $ pm2 start my-bash-script.sh-x -- interpreter bash $ pm2 start my-python-script.py-x -- interpreter python0 second stop overload: This feature allows you to reload code without losing request connections. Note: Only applicable to web applications running in Node 0.11.x in cluster mode (default mode) $ pm2 reload allCoffeeScript: $ pm2 start my_app.coffee # Is All PM2 ready for product-level service? Test $ git clone https://github.com/Unitech/pm2.git$ cd pm2 $ npm install # Or npm install -- dev on your server. If devDependencies is not installed $ npm test

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.