PM2 Practical Guide, Detailed PM2, PM2 easy script background, how Node runs in the background, how to get npm start running in the background

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_32281471/article/details/91369344

How to get Node code has been written to run in the background of it? Even if you close the command line can be run, how to do it? Here recommend a tool PM2

PM2 official website
PM2 github


Installation
start
to see the deployment of applications PM2
PM2 file directory
custom startup file


Here Insert Picture Description
Outline the explanation Xiaguan network

PM2 is an application manager, has its own load-balancing capability, which allows the application has been running in the background, but also allows seamless restart the application, the middle will not stop the service, you can boot, can greatly facilitate the developer terms application deployment, reduce maintenance costs.

installation

npm i -g pm2

start up

pm2 start app.js --watch   #实时监控app.js的方式启动,当app.js文件有变动时,pm2会自动reload
pm2 start app.js -i max  # 根据有效CPU数目启动最大进程数目
pm2 start app.js -n appname -i max -e err.log -o out.log # 以 appname 启动 app.js,错误当前目录 err.log 为 error log,out.log 为输出 log
pm2 start bb.sh  --interpreter bash # 用 bash 执行脚本
pm2 start test.py--interpreter python3 # 用 python 环境执行脚本

Start similar node xxx.jsprojects

pm2 start --name xxxsname xxx.js  # 将运行的实例命名为 xxxsname

Start similar npm startprojects

pm2 start --name appname npm -- start # 设置应用名为 appname

Start similar npm run serveprojects

pm2 start --name servename npm -- run serve # 设置应用名为 servename

--name xxxOr -n xxxsaid it would use the name xxx

Start in the form of profiles

pm2 start pm2config.json

View application deployment PM2

pm2 list               # 显示所有进程状态
pm2 ls                 # 显示所有进程状态
pm2 show 0			   # 显示某个应用的详细信息
pm2 monit              # 监视所有进程
pm2 logs               # 显示所有进程日志
pm2 log 0 	           # 查看 0 应用的日志
pm2 stop all           # 停止所有进程
pm2 restart all        # 重启所有进程
pm2 reload all         # 0秒停机重载进程
pm2 stop 0             # 停止指定的进程,0 是应用 id
pm2 restart 0          # 重启指定的进程,0 是应用 id
pm2 startup            # 产生 init 脚本 保持进程活着,startup 是指系统boot, 开机进程自启动
pm2 unstartup          # 禁用开机进程自启动
pm2 delete 0           # 杀死指定的进程,0 是应用 id,会删除该应用
pm2 delete all         # 杀死全部进程,会删除所有应用

pm2 list
Here Insert Picture Description
pm2 show
Here Insert Picture Description
pm2 monit
Here Insert Picture Description
pm2 logs
Here Insert Picture Description
pm2 log bb
Here Insert Picture Description

PM2 file directory

PM2 file default on $ HOME / .pm2
Here Insert Picture Description

Custom startup file

{
  "apps": [{
    "name": "appname", # 应用名
    "exec_interpreter": "node", # 执行环境
    "script": "./b.js", # 要执行的脚本
    "cwd": "/home/uftp/test-pm2", # 项目路径
    "exec_mode": "fork",
    "max_memory_restart": "1G",
    "autorestart": true, # 出错自动重启
    "node_args": [],
    "watch": false
    "error_file" : "./test-err.log", # 错误日志文件位置
    "out_file": "./test-out.log",  # 输出日志文件位置
    "pid_file": "./test.pid",  # 进程相关文件位置
    "env": {
       "NODE_ENV": "development"
    },
    "min_uptime": "60s",
    "max_restarts": 30
  }]
}

apps: Is an array, the array of each object represents a running application, you can configure multiple applications simultaneously start
exec_interpreterexecution environment, node bash pythonand so on
script: script to be executed
cwd: Path of application
exec_mode: The application startup mode, the default is forkalso can be set cluster_mode
max_memory_restart: will restart after exceeding the specified value of the application 1G 500M
autorestart: whether to automatically restart after failure, default true
watch: monitor file changes automatically restart after changes to the file
error_file: error log error log
out_file: out log output date
pid_file: process file
min_uptime: the shortest running time If less than this time quit, it will trigger max_restarts
max_restarts: the maximum number of restarts


No public attention sealing talk technology, gives technical information, your support is my greatest motivation
lxfriday_xyz

Guess you like

Origin blog.csdn.net/qq_32281471/article/details/91369344