pm2一个带有负载均衡功能的Node应用的进程管理器
PM2是一个提供了内置的负载均衡的Node.js应用程序进程管理器。它可以让你保持应用程序永远在线,无需停机地重启应用,也有助于系统管理员工作。
安装 PM2
$ npm install pm2 -g
启动应用
$ pm2 start app.js
这样,应用就已经在后台启动,并被监测保持在线。
模块系统
PM2 内嵌一个简单强大的模块系统,可以直接像下面这样安装模块
$ pm2 install <module_name>
- pm2-logrotate 日志管理及分割模块
- pm2-webshell 强大的浏览器模拟终端
- pm2-auto-pull 持续集成,自动拉取代码
更新PM2
# Install latest pm2 version
$ npm install pm2 -g
# Save process list, exit old PM2 & restore all processes
$ pm2 update
主要特性
命令概览
$ npm install pm2 -g # Install PM2
$ pm2 start app.js # Start, Daemonize and auto restart application
$ pm2 start app.js -i 4 # Start 4 instances of application in cluster mode
# it will load balance network queries to each app
$ pm2 start app.js --name="api" # Start application and name it "api"
$ pm2 start app.js --watch # Restart application on file change
$ pm2 start script.sh # Start bash script
$ pm2 list # List all processes started with PM2
$ pm2 monit # Display memory and cpu usage of each app
$ pm2 show [app-name] # Show all informations about application
$ pm2 logs # Display logs of all apps
$ pm2 logs [app-name] # Display logs for a specific app
$ pm2 flush
$ pm2 stop all # Stop all apps
$ pm2 stop 0 # Stop process with id 0
$ pm2 restart all # Restart all apps
$ pm2 reload all # Reload all apps in cluster mode
$ pm2 gracefulReload all # Graceful reload all apps in cluster mode
$ pm2 delete all # Kill and delete all apps
$ pm2 delete 0 # Delete app with id 0
$ pm2 scale api 10 # Scale app with name api to 10 instances
$ pm2 reset [app-name] # Reset number of restart for [app-name]
$ pm2 startup # Generate a startup script to respawn PM2 on boot
$ pm2 save # Save current process list
$ pm2 resurrect # Restore previously save processes
$ pm2 update # Save processes, kill PM2 and restore processes
$ pm2 generate # Generate a sample json configuration file
$ pm2 deploy app.json prod setup # Setup "prod" remote server
$ pm2 deploy app.json prod # Update "prod" remote server
$ pm2 deploy app.json prod revert 2 # Revert "prod" remote server by 2
$ pm2 module:generate [name] # Generate sample module with name [name]
$ pm2 install pm2-logrotate # Install module (here a log rotation system)
$ pm2 uninstall pm2-logrotate # Uninstall module
$ pm2 publish # Increment version, git push and npm publish
开启进程的多种方式
$ pm2 start app.js --watch # Restart application on file change
$ pm2 start script.sh # Start bash script
$ pm2 start app.js -- -a 34 # Start app and pass option -a 34
$ pm2 start app.json # Start all applications declared in app.json
$ pm2 start my-python-script.py --interpreter python
进程管理
列出所有进程:
$ pm2 list
管理进程
$ pm2 stop <app_name|id|'all'|json_conf>
$ pm2 restart <app_name|id|'all'|json_conf>
$ pm2 delete <app_name|id|'all'|json_conf>
查看进程的详情
$ pm2 describe <id|app_name>
CPU / 内存监测
$ pm2 monit
日志工具
pm2 logs ['all'|'PM2'|app_name|app_id] [--err|--out] [--lines <n>] [--raw] [--timestamp [format]]
例:
$ pm2 logs
$ pm2 logs WEB-API --err
$ pm2 logs all --raw
$ pm2 logs --lines 5
$ pm2 logs --timestamp "HH:mm:ss"
$ pm2 logs WEB-API --lines 0 --timestamp "HH:mm" --out
$ pm2 logs PM2 --timestamp
$ pm2 flush // Clear all the logs
开机启动
PM2可以自动生成并配置开机启动脚本,让服务器在重启的时候可以保持应用在线
$ pm2 startup
# auto-detect platform
$ pm2 startup [platform]
# render startup-script for a specific platform, the [platform] could be one of:
# ubuntu|centos|redhat|gentoo|systemd|darwin|amazon