Moment For Technology

PM2 Simple user manual

Posted on Dec. 1, 2022, 11:06 p.m. by Christina Gutierrez
Category: reading Tag: javascript node.js Load balancing PM2

PM2 User manual

It is very simple and very water, that is, I happen to be studying this thing today because of the company project, just as my own record, don't spray if you don't like ~ ~ ~ thank you very much


For online projects, if the project is started directly through the Node app, if an error is reported, it may directly stop and cause the whole service to crash. Generally, there are several solutions for monitoring node.

  • Supervisor: Normally used in the development environment.
  • Forever: Managing multiple sites, often with small traffic to each site, no monitoring is necessary.
  • PM2: The website has a large number of visits and requires a complete monitoring page.

The original project of the company adopted the form of Forever, but if the node has problems, there is no way to obtain effective monitoring data for error troubleshooting, so the newly developed system plans to adopt the form of PM2 to monitor the front end and node layer.

Main features of PM2

  • Built-in load balancing (using Node Cluster module)
  • The background
  • No downtime is required for maintenance upgrades.
  • Have startup scripts for Ubuntu and CentOS
  • Stop unstable processes (avoid infinite loops)
  • Console inspection
  • The HTTP API
  • Remote control and real-time interface API (Nodejs module, allows interaction with PM2 process manager)

The installation

// Install pm2 globally, depending on node and NPM NPM install -g pm2Copy the code


  • Basic start command

pm2 start

// The start command starts the corresponding node server file $pm2 start. /build/server.jsCopy the code
  • Starting with a configuration file is explained in more detail later

After startup, the console will see the following message:

As shown in the figure above, you can see the project Kafazhe starts successfully with id 0 and the status is online.

  • View detailed status information

pm2 show (appname|id)

$ pm2 show kaifazhe
Copy the code

As shown in the figure above, you can view the details of the Kaifazhe process

  • View the list of all started processes

pm2 list

$ pm2 list
Copy the code

  • Monitors CPU and memory usage of each node process

    pm2 monit

$ pm2 monit
Copy the code

You can use the PM2 monit function to monitor the running status of all node processes, including various responses and error messages.

  • The logs of all processes are displayed

pm2 logs

$ pm2 logs
Copy the code

  • Monitor the state of the machines on which these processes are running

pm2 web

$ pm2 web
Copy the code

All I can say is, it's amazing that you can monitor not only the processes, but also the state of the machines that run them. Then it will automatically start a service on the specified port, as shown in figure 9615 to start a service that we can access. Although I do not understand, but for testing the operation of students, it should be very useful.

  • Stop specified/all processes

pm2 stop (id|all)

$pm2 stop 0 $pm2 stop all $pm2 stop allCopy the code

As shown in the figure, we ran both services online. After using stop 0, Kaifazhe's service became stopped, and then using stop ALL, all processes became stopped.

  • Restart specified/all processes

pm2 restart (id|all)

$pm2 restart 0 $pm2 restart all $pm2 restart allCopy the code
  • Kills specified/all processes

pm2 delete (id|all)

$pm2 delete 0 $pm2 delete all $pm2 delete allCopy the code

As you can see from the figure above, after restart 0, process 0 goes from stopped to online. Then delete 0 and process 0 disappears. Then delete all and you can see that no process is running.

Configure the PM2 startup file

The pM2 startup mode can be extended to many advanced functions, such as setting the environment, setting error information printing, setting input information printing and so on. Then a single command can't do these tasks. All pM2 provides a configuration file to start ~


Module. exports = {apps: [{name:]'kaifazhe'// apply the name script:'./build/server.js', // start file address CWD:'/', // Current working path watch: [// monitor the changing directory, once changed, automatically restart'src'.'build',], ignore_watch: [// Ignore these directory changes'node_modules'.'logs'.'public',
      node_args: '--harmony', // Node boot mode env: {NODE_ENV:'development'In this case, process.env.node_env is the value of development ORIGIN_ADDR:''
      env_production: {
        NODE_ENV: 'production',
      out_file: './logs/out.log', // Common log path error_file:'./logs/err.log'// Error log path merge_logs:true,
      log_date_format: 'YYYY-MM-DD HH:mm Z',}]};Copy the code

For env above, we can internally add multiple parameter variables so that the we use will change accordingly. For example, above, our process.env.origin_addr value is ~

Load balancing

The 666 feature is here to automatically do load balancing for you with a single command, and it doesn't matter if you don't understand the complex concepts.

pm2 start server.js -i (number|max)

Start three processes to run the project
pm2 start app.js -i 3
# Start the corresponding number of processes to run the project according to the number of CPU cores on the machine
pm2 start app.js -i max
Copy the code

Configuration files in the corresponding: "instance" : (number | Max)

// pm2.config.js
"instances": 2, // Start two instancesCopy the code

Logs related

Here is after a year after the supplement, originally just to write a simple summary, but did not expect so many people like, so a year later as the use of deepening, for PM2 has an other summary, I will add ~

Pm2 log

As you can see from the configuration file above, we can configure logs, including both normal OUT and error logs. There is no need for us to do anything, we just need to configure it in config and it will automatically log in:

Very simple function, including the log, is really wonderful, but is it really that wonderful? Ha ha, don't you think? All of our logs are exported to err. Log and out.log

Log segmentation

For example, node logs are written by date using log4js. Can pm2 be written by date? The answer must be: yes.

Pm2 provides us with a plug-in system, and the date segmentation function is in use of the plug-in system: PM2-Logrotate


Pm2 install pm2-logrotate pm2-logrotateCopy the code

It starts automatically after you install it, and then you can configure various parameters

The log is then split by date ~

It is said that there is a bug in the official PM2-logrotate, which is that the dates are divided normally. However, if your file from the previous day is not full, for example, if you set 1 MB but only write 500K, then the next day's log will still be inserted into the original out.log(err.log), so he wrote this to solve this problem: m2-logrotate-ext

With PM2-Web to achieve monitoring visualization

Perhaps many people do not like the console, like the monitoring data visualization is more convenient to view and analysis. It does not matter, the masters have provided us with tools, PM2-Web, a look is specialized with THE use of PM2.

The installation

npm install -g pm2-web
Copy the code


By default, PM2-Web automatically starts a port 8080, but we prefer controlled state, so start as a configuration file.

$ pm2-web --config pm2-web-config.json
Copy the code
// pm2-web-config.json
  "www": {
      "host": "localhost"."address": ""."port": 6688}}Copy the code

This allows you to view the monitoring status visually in your browser

The reason is that pc2-web relies on Nod-inspector, and nod-inspector does not allow you to install a higher version of Node. Many people have raised the issue, but the developers seem to have given up. I'm not going to install an earlier version of Node locally, so if you're interested, you can try it out

Thank you

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.