pm2
PM2 is a daemon process manager that will help you manage and keep your application online.
1、安装 node 环境
cd /usr/local/
wget https://nodejs.org/dist/v12.18.3/node-v12.18.3-linux-x64.tar.xz
unxz -kv node-v12.18.3-linux-x64.tar.xz
tar xvf node-v12.18.3-linux-x64.tar
mv node-v12.18.3-linux-x64 node
echo 'export PATH=/usr/local/node/bin:$PATH' > /etc/profile.d/node.sh
source /etc/profile.d/node.sh
2、安装 pm2
npm install pm2 -g
yarn global add pm2
3、查看用法
pm2 examples
4、命令行补全
pm2 completion install
5、常用命令
pm2 start app.js --name app_name
pm2 ls
pm2 stop app_name
pm2 restart app_name
pm2 delete app_name
Instead of app_name you can pass:
all
to act on all processesid
to act on a specific process id
Ensure pm2 daemon has been launched
ping pm2 daemon - if not up it will launch it.
pm2 ping
kill the current pm2
pm2 kill
brings back previously saved processes via pm2 save
pm2 resurrect
Start maximum processes with LB depending on available CPUs
pm2 start app.js -i 0
pm2 start app.js -i max
Scales app
up by 3 workers
pm2 scale app +3
Scales app
up or down to 2 workers total
pm2 scale app 2
内存限制
pm2 start api.js --max-memory-restart 300M
6、开机自启动
pm2 startup
pm2 unstartup
保存状态用于自启动或恢复
pm2 save
7、升级
npm install pm2@latest -g
pm2 update
8、查看日志
pm2 logs
pm2 logs --lines 200
9、终端监控
pm2 monit
10、图形UI
pm2 plus
11、使用示例
pm2 start "java -jar cmp-registry.jar" --name cmp-registry # 启动一个服务并指定服务名称
pm2 ls # 查看服务状态
pm2 startup # 开启自启动
pm2 save # 保存状态
参考: