nodejs pm2 应用指南

时间:2022-10-18 18:01:11

1.安装配置

pm2是一个带有负载均衡功能的应用进程管理器,类似有Supervisor,forever,详细参数见官网:​​http://pm2.keymetrics.io​

1、安装 nodejs 和 pm2

curl -sL https://rpm.nodesource.com/setup_8.x | bash -
wget https://nodejs.org/dist/latest-v12.x/node-v12.22.8-linux-x64.tar.gz
tar zxvf node-v12.22.8-linux-x64.tar.gz
mv node-v12.22.8-linux-x64 /usr/local/node
ln -sv /usr/local/node/bin/node /usr/local/bin/node
ln -sv /usr/local/node/bin/npm /usr/local/bin/npm
npm config set registry https://registry.npm.taobao.org
yum install -y nodejs
npm install pm2@latest -g

2.常用指令

2.1.启动

pm2 start app.js
pm2 start app.js --name my-api #my-api为PM2进程名称
pm2 start app.js -i 0 #根据CPU核数启动进程个数
pm2 start app.js --watch #实时监控app.js的方式启动,当app.js文件有变动时,pm2会自动reload

2.2.查看进程

pm2 list
pm2 show 0 或者 # pm2 info 0 #查看进程详细信息,0为PM2进程id

2.3.监控

pm2 monit

2.4.停止

pm2 stop all
pm2 stop 0

2.5.重新加载

pm2 reload all
pm2 reload 0

2.6.重启

pm2 restart all
pm2 restart 0

2.7.删除

pm2 delete 0
pm2 delete all

2.8.日志操作

pm2 logs [–raw]
pm2 flush
pm2 reloadlogs

2.9.升级版本

npm install pm2@lastest -g
pm2 updatePM2

3.自启动

sudo -u postgres pm2 start app.js --name konga
sudo -u postgres pm2 startup
sudo -u postgres pm2 save

4.启动和管理 jar

4.1.配置文件编写

vim datax-admin.json


{
"apps" : {
"name":"datax-admin",
"script":"java",
"args":[
"-Xmx1024M",
"-Xms1024M",
"-Xmn448M",
"-XX:MaxMetaspaceSize=192M",
"-XX:MetaspaceSize=192M",
"-jar",
"datax-admin-2.1.1.jar",
],
"error_file":"./log/datax-admin-err.log",
"out_file":"./log/datax-admin-out.log",
"merge_logs":true,
"log_date_format":"YYYY/MM/DD HH:mm:ss",
"min_uptime": "60s",
"max_restarts": 30,
"autorestart": true,
"restart_delay": "60"
}
}

4.2.带变量的配置文件

node build/app.js server --port=8081
/usr/bin/nohup /usr/local/node/bin/node /var/lib/grafana/plugins/grafana-image-renderer/build/app.js server --port=8081 &


{
"apps" : {
"name":"image-renderer",
"script":"node",
"args":[
"/var/lib/grafana/plugins/grafana-image-renderer/build/app.js",
"server",
"--port=8081",
],
"error_file":"/tmp/image-renereer-err.log",
"merge_logs":true,
"log_date_format":"YYYY/MM/DD HH:mm:ss",
"min_uptime": "60s",
"max_restarts": 30,
"autorestart": true,
"restart_delay": "60"
}
}

4.3.启动

pm2 start datax-admin.json

nodejs pm2 应用指南

nodejs pm2 应用指南