** Preamble: ** Since Node is a single thread, if a service dies, the service thread dies. Pm2 is a process management tool that you can use to manage your Node processes.

Main functions of the PM2

  • Built-in load balancer (using Node Cluster cluster module)
  • The background
  • 0 second downtime reloading, I understand it probably means that maintenance and upgrade does not need downtime.
  • 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)

1. Install pM2 globally

Run the command in any directory. The pM2 installation directory is in the bin folder in the Node installation directory.

npm install -g pm2
Copy the code

And then it will say WARN, so don’t worry about that

/ node/node_install_path/node - v8.11.0 - Linux - x64 / bin/pm2 - > / node/node_install_path/node - v8.11.0 - Linux - x64 / lib/node_modules/pm2 / bin/pm2 / node/node_install_path/node - v8.11.0 - Linux - x64 / bin/pm2 dev - > / node/node_install_path/node - v8.11.0 - Linux - x64 / lib/node_modules/pm2 / bin/pm2 - dev / node/node_install_path/node - v8.11.0 - Linux - x64 / bin/pm2 docker - > / node/node_install_path/node - v8.11.0 - Linux - x64 / lib/node_modules/pm2 / bin/pm2 - docker / node/node_install_path/node - v8.11.0 - Linux - x64 / bin/pm2 - runtime - > /node/node_install_path/node-v8.11.0-linux-x64/lib/node_modules/pm2/bin/pm2- Runtime NPM WARN Registry Using stale Package the data from https://registry.npmjs.org/ due to a request error during revalidation. + [email protected] added 221 packagesin81.645 sCopy the code

2. Pm2 Configure global variables

The path is node installation path bin path

ln -s/ node/node_install_path/node - v8.11.0 - Linux - x64 / bin/pm2 / usr /local/bin/pm2
Copy the code

3. Check whether environment variables are configured

Pm2 -v // PM2 2.10.2 is displayed normallyCopy the code

4. Set pM2 startup

pm2 startup
Copy the code

5. Basic use of PM2

Pm2 start app.js — Watch Automatically restarts when the code changes pm2 restart app.js Restart pm2 stop app.js Stop pm2 log Displays all process logs

See the official documentation for more pM2 usage

Related articles

  • [Node] CentOs Set up the NodeJs server. – Install the Node
  • [Node] CentOs Set up NodeJs server – Install Mongodb