preface


Source: github.com/Unitech/pm2

The PM2 here is not an air pollutant

In fact, PM2 is a process manager for Node applications with load balancing capabilities. Node has many libraries for process management, and Forever is one of the powerful but relatively old process managers.

Why pM2


The basic reason for this is that Node is a single-threaded application, which is characterized by all methods being executed sequentially, and Node does not have the ability to create a new thread for asynchronous operations like Java does. Blocking during I/O execution reduces the efficiency of the entire application, resulting in high CPU usage.

Therefore, in this mode, a thread can only handle one task, and to improve throughput you must use multiple threads. Although there are many advantages of single thread, such as avoiding thread synchronization or deadlock, state synchronization, etc., the biggest disadvantage of single thread is that it cannot take advantage of multi-core CPU to improve operation efficiency in today’s increasingly multiplying application and computing power requirements.

We know that Node can take advantage of asynchronous I/O to avoid thread blocking and improve utilization, etc. :

At the same time, in order to make up for the problem that single thread cannot use multi-core CPU, the concept of “child process” is provided. Node.js is actually a single thread of JavaScript execution thread, the real I/O operation, the underlying API calls are executed through multiple threads. Of course I’m just pointing out the context here, so why do we use PM2?

Pm2 deploys your application to all cpus on the server ($pm2 start app.js -i Max), effectively solving the problem mentioned in the previous background. 2. Why not use Forever as a process manager? I think the biggest difference is the lack of monitoring and limited process and cluster management. Forever beats PM2 in terms of monitoring and logs, and you can see why in one picture.

Here’s the pM2 Demo, where you can see the cluster’s patterns, state, CPU utilization, and even memory size. What does Forever say? null

What if I want to monitor all process status in real time?

This is the power of PM2, multi-process management, monitoring, load balancing…

Main features of PM2


  • Built-in load balancing (using Node Cluster cluster modules and sub-processes, please refer to Chapter 9 of Simple Node.js by Ling Pu)
  • Thread daemon, keep alive
  • 0 seconds stop overload, maintenance and upgrade do not need to stop.
  • Now Linux (stable) & MacOSx (stable) & Windows (stable). Multi-platform support
  • Stop unstable processes (to avoid infinite loops)
  • Console detection
  • The HTTP API
  • Remote control and real-time API (Nodejs module, allowing interaction with PM2 process manager)

usage


$NPM install pm2 -g # install pm2 $pm2 start app.js -i 4 # install pm2 $pm2 start app.js --name my-api # Name process $pm2 list # Display all process status $ $pm2 restart all $pm2 restart all $pm2 reload all $pm2 restart all $pm2 restart all $pm2 reload all (For NETWORKED processes) $pm2 stop 0 # Stop the specified process $pm2 restart 0 # Restart the specified process $pm2 startup # Generate init script to keep the process alive $pm2 Web # Run robust Computer API endpoint (http://localhost:9615) $pm2 delete 0 $pm2 delete all $pm2 start app.js -i Max # $pm2 start app.js -i 3 # Start 3 processes $pm2 start app.js -x # fork app.js Instead of using cluster $pm2 start app.js -x -- a 23 # fork app.js and pass parameters (-a 23) $pm2 start app.js --name serverone # $pm2 start app.json $pm2 start app.json $pm2 start app.js -i Max -e err.log -o out.log # You can also execute apps written in other languages (fork mode): $ pm2 start my-bash-script.sh -x --interpreter bash $ pm2 start my-python-script.py -x --interpreter pythonCopy the code

Keymetrics real-time monitoring is attached


Address: app. Keymetrics. IO / # / register

Pm2 official also combined with PM2 management to provide a set of visual online monitoring platform, how awesome? Above:

After registering and creating your first project, you will get the password as shown in the picture:

Public key and secret key assigned by the system. Then, enter the following command on the server where pM2 is installed

pm2 interact your-secret-key your-public-key
Copy the code

Visualization will take care of the rest.