一、安装pm2
注意:下面的所有操作都是在
ubuntu:20.04.6服务器系统下进行,同时确保nodejs和npm环境,可以用命令检查
$ node -v
v20.11.1
$ npm -v
10.2.4
然后通过npm安装全局pm2。
$ npm install pm2 -g
npm WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
added 162 packages in 3m
同样可以通过pm2 -v检查版本
$ pm2 -v
-------------
__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
_\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
_\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
_\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
_\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
_\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
_\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
_\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
_\///______________\///______________\///__\///////////////__
Runtime Edition
PM2 is a Production Process Manager for Node.js applications
with a built-in Load Balancer.
Start and Daemonize any application:
$ pm2 start app.js
Load Balance 4 instances of api.js:
$ pm2 start api.js -i 4
Monitor in production:
$ pm2 monitor
Make pm2 auto-boot at server restart:
$ pm2 startup
To go further checkout:
http://pm2.io/
-------------
[PM2] Spawning PM2 daemon with pm2_home=/home/rcz/.pm2
[PM2] PM2 Successfully daemonized
5.3.1
二、使用pm2
为了可以演示pm2管理nodejs项目,笔者已经在gitee准备了一个简易项目,仓库地址
注意:确保操作系统已经具备
git环境,有关git部分的知识,可以自行网上查询学习,笔者默认读者都是git的基本使用者。
准备一个code目录并进入,拉取代码并进入代码目录。
$ git clone https://gitee.com/renfax/hello-express.git
$ cd hello-express
通过pm2 start index.js启动应用程序
$ pm2 start index.js
[PM2] Starting /home/rcz/code/hello-express/index.js in fork_mode (1 instance)
[PM2] Done.
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name │ mode │ ↺ │ status │ cpu │ memory │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0 │ index │ fork │ 0 │ online │ 0% │ 40.9mb │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
通过pm2 start 0终止应用程序,0为上面启动程序的id
pm2 stop 0
[PM2] Applying action stopProcessId on app [0](ids: [ '0' ])
[PM2] [index](0) ✓
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name │ mode │ ↺ │ status │ cpu │ memory │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0 │ index │ fork │ 0 │ stopped │ 0% │ 0b │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
可以看出通过0这样的id管理应用进程不够直观,可以通过pm2的--name参数来为一个进程赋予一个名称。
例如:
pm2 start index.js --name helloexpress
[PM2] Applying action restartProcessId on app [helloexpress](ids: [ 1 ])
[PM2] [helloexpress](1) ✓
[PM2] Process successfully started
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name │ mode │ ↺ │ status │ cpu │ memory │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 1 │ helloexpress │ fork │ 15 │ online │ 0% │ 24.6mb │
│ 0 │ index │ fork │ 0 │ stopped │ 0% │ 0b │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
那么之后就可以通过pm2 stop helloexpress终止它。
$ pm2 stop helloexpress
[PM2] Applying action stopProcessId on app [helloexpress](ids: [ 1 ])
[PM2] [helloexpress](1) ✓
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name │ mode │ ↺ │ status │ cpu │ memory │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 1 │ helloexpress │ fork │ 15 │ stopped │ 0% │ 0b │
│ 0 │ index │ fork │ 0 │ stopped │ 0% │ 0b │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
我们还可以通过pm2 reload <标识符>来重启一个程序,<标识符>既可以是一个应用进程的id也可以是应用进程的名称。
如下:
$ pm2 reload helloexpress
Use --update-env to update environment variables
[PM2] Applying action reloadProcessId on app [helloexpress](ids: [ 1 ])
[PM2] [helloexpress](1) ✓
我们检查下,发现已经重启了。
$ pm2 ls
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name │ mode │ ↺ │ status │ cpu │ memory │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 1 │ helloexpress │ fork │ 15 │ online │ 0% │ 53.9mb │
│ 0 │ index │ fork │ 0 │ stopped │ 0% │ 0b │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
现在我们发现最开始启动的id为0的应用进程不需要了,我们可以通过pm2 delete <标识符>删除它。
$ pm2 delete 0
[PM2] Applying action deleteProcessId on app [0](ids: [ '0' ])
[PM2] [index](0) ✓
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name │ mode │ ↺ │ status │ cpu │ memory │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 1 │ helloexpress │ fork │ 15 │ online │ 0% │ 54.9mb │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
以上就是pm2的基础用法,有关更多相关只是,请访问pm2官方网站。