PM2 是具有内置负载均衡器的 Node.js 应用程序的生产过程管理器。它允许您使应用程序永远保持活动状态,在不停机的情况下重新加载它们,并促进常见的系统管理任务。
npm install pm2 -g
自动监听文件变化
pm2 start app.js --watch
pm2 start app.js # 启动
pm2 stop app.js # 停止
pm2 logs # 日志
pm2 stop all # 停止全部
pm2 delete all # 杀死全部进程
pm2 startup # 开机自启
pm2 update pm2 # 更新 pm2
pm2 serve ./dist 9090 # 将目录dist作为静态服务器根目录,端口为9090
pm2 list # 查看启动列表
pm2 monit # 查看每个应用程序占用情况
pm2 ecosystem # 生成一个示例JSON配置文件
pm2 init
todo:停止脚本待测试
都安装在一个宿主机中,或都安装在一个容器中
cuiwei@weideMacBook-Pro laravel-demo % cat process.yml
apps:
- name: "laravel:queue:work" # 这里自己命名
script: artisan #指定脚本为 artisan 脚本
watch: false # 不监听文件变化
interpreter: php # 脚本为php,如果你的 php 不在全局变量可以指定绝对路径脚本
args: "queue:work --tries=3" # artisan 命令和参数
restart_delay: 3000 # 进程中断三秒后重启
error_file: ./storage/logs/queue.error.log # 错误日志
out_file: ./storage/logs/queue.out.log # 输出日志
pid_file: ./storage/app/queue.pid # pid 文件路径
cuiwei@weideMacBook-Pro laravel-demo % cat process2.yml
apps:
- name: "laravel:queue:work" # 这里自己命名
script: docker_artisan.sh #指定脚本为 artisan 脚本
watch: false # 不监听文件变化
interpreter: bash # 使用 Bash 解释器来执行命令
args: "" # artisan 命令和参数
restart_delay: 3000 # 进程中断三秒后重启
error_file: ./storage/logs/queue.error.log # 错误日志
out_file: ./storage/logs/queue.out.log # 输出日志
pid_file: ./storage/app/queue.pid # pid 文件路径
pre-stop: docker_artisan_clear.sh # 停止脚本
cuiwei@weideMacBook-Pro laravel-demo % cat docker_artisan.sh
#!/usr/bin/env bash
docker exec -u www-data server-docker-php-fpm-1 /var/www/laravel-demo/artisan queue:work --tries=3
cuiwei@weideMacBook-Pro koa-demo % pm2 init simple
-------------
__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
_\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
_\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
_\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
_\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
_\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
_\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
_\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
_\///______________\///______________\///__\///////////////__
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=/Users/cuiwei/.pm2
[PM2] PM2 Successfully daemonized
File /Users/cuiwei/PhpstormProjects/koa-demo/ecosystem.config.js generated
https://github.com/Unitech/pm2
https://blog.csdn.net/qq_41008918/article/details/118439088