Pm2 is a Process management tool based on Node. Pm2 can easily manage startup processes, such as performance monitoring and automatic restart.

The installation

$ npm install pm2@latest -g
# or
$ yarn global add pm2
Copy the code

Start an app

The startup method is extremely simple.

$ pm2 start app.js
Copy the code

As a process manager, PM2 can naturally start tasks for other processes.

$ pm2 start bashscript.sh
$ pm2 start python-app.py --watch
$ pm2 start binary-file -- --port 1520
Copy the code

There are some optional arguments for start

You can use an id or an application name to manipulate the application
--name <app_name>

Restart the application when the file changes
--watch
 # Set the memory threshold for overloaded applications --max-memory-restart <200MB>  # specify a log file --log <log_path>  Pass extra arguments to the script -- arg1 arg2 arg3  Automatic restart interval --restart-delay <delay in ms>  # time log prefix --time  # Do not automatically restart the application --no-autorestart   Copy the code

Management process

$ pm2 restart app_name
$ pm2 reload app_name
$ pm2 stop app_name
$ pm2 delete app_name
Copy the code

App_name can also pass id or all to perform all

The difference between Reload and restart is

  • Reload implements zero second stop for overloaded processes
  • If reload fails, restart is executed

List all current apps

$ pm2 [list|ls|status]
Copy the code

Displaying Real-time Logs

$ pm2 logs
Copy the code

Terminal panel

$ pm2 monit
Copy the code

A web interface

$ pm2 plus
Copy the code

There is a problem with this interface and it will not start.

Practical cases

Take a NUXT SSR project as an example

// Build the project$ yarn bulid
// Start the service$ pm2 start "yarn start" --name website

Copy the code
image

And you can see our application is up.

See the log

image

Viewing Real-time Monitoring

Start from the configuration file

$ pm2 ecosystem
Copy the code

Let’s modify the configuration file. The specific configuration parameters are set here

module.exports = {
  apps : [{
    name:"website"
    script: 'yarn start'.    watch: '. '. out_file: 'log.info'  } ].}; Copy the code

Start the service

$ pm2 delete website
$ pm2 start ecosystem.config.js 
Copy the code

Here you need to delete the original application and restart it.

conclusion

Through PM2 we can better manage the application, more intuitive to see the application resource monitoring, and automatic restart, batch start and other functions as well as cluster function due to the limited time will not be more introduced, home deployment must be a good product.