pm2 常用快捷键
设置服务器重启时自动启动
pm2 startup
pm2 管理node项目
step1:install pm2npm install pm2@latest -gstep2:常用命令 项目名称以ORION为例//首次启动pm2 start app.js --name ORION//以后再启动pm2 start ORION//重启pm2 restart ORION//关闭pm2 stop ORION//查看所有的项目pm2 list//stop all processespm2 stop all// restart all processespm2 restart all启动某个script 如: pm2-start-prodpm2 start "npm run pm2-start-prod" --name backend-prod// pm2 启动 npm startpm2 start npm --name "Your APP Name" -- starteg:pm2 start npm --name "sms-processor" -- start//pm2 启动package.json 里面的 developpm2 start npm --name "project-name" -- run developpm2 启动package.json 里面 名为littleAppleBot 的scriptpm2 start --name "app_name" npm -- run littleAppleBot --eg:pm2 start --name "mp" npm -- run develop --指定环境变量NODE_ENV=production pm2 start server.js --name apieg:PORT=28004 pm2 start --name "strapi-blog-28004" npm -- run develop --// .sh 文件没法执行,需要给权限chmod +x ./test.shpm2 启动 python脚本pm2 start main.py --interpreter /usr/local/bin/python3.7 --name proxy_pool也可以使用 ecosystem.config.js 启动项目使用pm2 init simple创建ecosystem.config.js 文件配置案例module.exports = {apps : [{name : "app1",script : "./app.js",ignore_watch: ["node_modules", "output", "temp","download", "\\.git", "*.log"],env_production: {NODE_ENV: "production"},env_development: {NODE_ENV: "development"}}]}pm2 start ecosystem.config.js --only api-app// 开启watchpm2 start ecosystem.config.js --only api-app --watch
Auto restart apps on file change
// 加上 --watchpm2 start app.js --watch
pm2 日志管理
default log location ~/.pm2/logshttps://github.com/keymetrics/pm2-logrotate#configurepm2 install pm2-logrotate// force rotate every minutepm2 set pm2-logrotate:rotateInterval '*/1 * * * *'// rotate every 1K (1KB)pm2 set pm2-logrotate:max_size 1K// 设置保留的文件个数,默认30个pm2 set pm2-logrotate:retain 300// 查看已有的配置pm2 config pm2-logrotateif you want to specify the log locationpm2 start npm --name "sms-processor" -o '/Users/anthony.lu/project/logs/sms-processor-out.log' -e '/Users/anthony.lu/project/logs/sms-processor-error.log' -- start// 删除日志pm2 flush # Clear all the logs// See logs of one projectpm2 logs 'project-name'// 或者使用项目idpm2 logs -1// 查看 1000 行pm2 logs appName --lines 1000// 仅查看 错误信息pm2 logs appName --err --lines=100
删除、保存、重启某个项目,
//删除某个项目pm2 delete ORIO//remove all processespm2 delete all//Saving current process listpm2 save//This brings back previously saved processespm2 resurrect//Generating a startup script(开机重启)pm2 startup//Remove init script viapm2 unstartup systemd