当前位置:网站首页>Nodejs imook PM2

Nodejs imook PM2

2020-11-10 10:44:00 My sister is talking nonsense

11.1 pm2

The online environment :

  • Server stability ( No matter how the user operates , The service can't be hung up , Something is wrong , It can't affect the use of other functions )
  • Make full use of server hardware resources , In order to improve performance ( Make full use of the hardware resources of the server )
  • Online logging ( Custom log , The operation log ,access-log Access log

PM2 Process daemons

  • The system crashes and restarts automatically
  • Multi process , Make full use of resources
  • With logging function
// package.json
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "cross-env NODE_ENV=dev nodemon app.js",
    "prd": "cross-env NODE_ENV=production pm2 start app.js"
  },

image.png

11.2 Common commands

pm2 start
pm2 list
pm2 restart name/id //   The above figure is an example :pm2 restart 1 
pm2 stop/delete 1
pm2 info 1
pm2 log 1
pm2 monit 1 

11.3 Process daemons

  • node app.js and nodemon app.js Process breakdown , Cannot access
  • pm2 Encountered a process crash , It will restart automatically ( Here's the picture )

image.png

11.4 Configuration item

  • newly build PM2 The configuration file ( Including the number of processes , Log file directory, etc )
  • modify PM2 Start command , restart
  • visit server, Check the contents of the log file ( Whether the logging is effective )
{
    "apps": {
        "name": "pm2-test-server",
        "script": "app.js",
        "watch": true,
        "ignore_watch": [
            "node_modules",
            "logs"
        ],
        "error_file": "logs/err.log",
        "out_file": "logs/out.log",
        "log_date_format": "YYYY-MM-DD HH:mm:ss" //  Add a time stamp to each log 
    }
}
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "cross-env NODE_ENV=dev nodemon app.js",
    "prd": "cross-env NODE_ENV=production pm2 start pm2.config.json"
  },

The file is written to the log :
image.png

11.5 Multi process

Single process memory is limited , The operating system limits the maximum available memory for a process .

  • The operating system limits the memory of a process
  • Memory : Can't make full use of all the memory of the machine
  • CPU: Can't make full use of multi-core CPU The advantages of
    package.json add to "instances": 4,

    image.png

版权声明
本文为[My sister is talking nonsense]所创,转载请带上原文链接,感谢