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
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.js
projects
pm2 start --name xxxsname xxx.js # 将运行的实例命名为 xxxsname
Start similar npm start
projects
pm2 start --name appname npm -- start # 设置应用名为 appname
Start similar npm run serve
projects
pm2 start --name servename npm -- run serve # 设置应用名为 servename
--name xxx
Or -n xxx
said 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
pm2 show
pm2 monit
pm2 logs
pm2 log bb
PM2 file directory
PM2 file default on $ HOME / .pm2
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_interpreter
execution environment, node
bash
python
and so on
script
: script to be executed
cwd
: Path of application
exec_mode
: The application startup mode, the default is fork
also 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