Characteristics and usage of PM2

Keywords: Programming Python JSON Linux Mac

Main features of pm2

  • Built in load balancing (for the use of Node cluster cluster module and subprocesses, please refer to Chapter 9 of Park Ling's "getting to the bottom of node.js")
  • Thread guard, keep alive
  • 0 second shutdown and heavy load, no need to shut down for maintenance and upgrading
  • Now Linux (stable) & Mac OS X (stable) & windows (stable). Multi platform support
  • Stop unstable processes (avoid infinite loops)
  • Console detection
  • Provide HTTP API
  • Remote control and real-time interface API (nodejs module, allowing interaction with PM2 process manager)


usage

$ npm install pm2 -g             # Command line installation pm2 
$ pm2 start app.js -i 4          # Run pm2 in the background and start 4 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 # Naming process
$ pm2 list                       # Show all process status
$ pm2 monit                      # Monitor all processes
$ pm2 logs                       #  Show all process logs
$ pm2 stop all                   # Stop all processes
$ pm2 restart all                # Restart all processes
$ pm2 reload all                 # 0 seconds to shut down the overloaded process (for the NETWORKED process)
$ pm2 stop 0                     # Stop the specified process
$ pm2 restart 0                  # Restart the specified process
$ pm2 startup                    # Generate init script to keep process alive
$ pm2 web                        # Run a robust computer API endpoint (http://localhost:9615)
$ pm2 delete 0                   # Kill the specified process
$ pm2 delete all                 # Kill the whole process

//Different ways to run a process:
$ pm2 start app.js -i max            # Start the maximum number of processes based on the number of effective CPU s
$ pm2 start app.js -i 3              # Start 3 processes
$ pm2 start app.js -x                # Start app.js in fork mode instead of cluster
$ pm2 start app.js -x -- -a 23       # Start app.js in fork mode and pass the parameter (- a 23)
$ pm2 start app.js --name serverone  # Start a process and name it serverone
$ pm2 stop serverone                 # Stop the serverone process
$ pm2 start app.json                 # Start the process and set options in app.json
$ pm2 start app.js -i max -- -a 23   # Pass parameters to app.js after
$ pm2 start app.js -i max -e err.log -o out.log  # Start and generate a profile

//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 python

Posted by ratebuster on Fri, 13 Dec 2019 12:37:05 -0800