@dungan
2020-11-03T00:53:00.000000Z
字数 1519
阅读 92
Linux
Supervisor 是一个 C/S 模型的程序,supervisord 是 server 端,supervisorctl 是 client 端。
通过 Supervisor 管理的进程,当进程意外被 Kill 时,Supervisor 会自动将它重启,可以很方便地做到进程自动恢复的目的,而无需自己编写 shell 脚本来管理进程。
yum install ‐y epel‐release
yum install ‐y supervisor
supervisord 的配置文件默认位于 /etc/supervisord.conf
。
; supervisor config file
...
...
; include 项加载的是需要管理的进程的配置文件
[include]
files = /etc/supervisor/conf.d/*.conf;
上一步Supervisor的配置文件中我们指定/etc/supervisor/conf.d/*.conf
为被管理进程的配置文件,内容如下:
# 新建一个应用并设置一个名称,这里设置为 hyperf
[program:hyperf]
# 设置命令在指定的目录内执行
directory=/var/www/hyperf/
# 设置环境变量
environment=PATH="/home/app_env/bin"
# 这里为您要管理的项目的启动命令
command=php ./bin/hyperf.php start
# 以哪个用户来运行该进程
user=root
# supervisor 启动时自动该应用
autostart=true
# 进程退出后自动重启进程
autorestart=true
#把 stderr 重定向到 stdout,默认 false
redirect_stderr=true
# 进程持续运行多久才认为是启动成功
startsecs=1
# 重试次数
startretries=3
# stderr 日志输出位置,标准错误日志
stderr_logfile=/var/www/hyperf/runtime/stderr.log
# stdout 日志输出位置,标准输出日志
stdout_logfile=/var/www/hyperf/runtime/stdout.log
配置文件修改完毕后,就可以启动 supervisor 服务了。
supervisord ‐c /etc/supervisord.conf
当用supervisor监护进程时,被监护进程不能是守护进程,这是由于守护进程通常会在fork完子进程后就让父进程”结束生命”,也即由supervisor创建的父进程退出,此时,supervisor无法再监护已退出进程创建出来的子进程。
supervisorctl 是 supervisord 的命令行客户端工具,使用的配置和 supervisord 一样,supervisorctl 进入 supervisorctl 的 shell 交互界面,也可以直接通过 shell 命令操作。
# 启动 hyperf 应用
supervisorctl start hyperf
# 重启 hyperf 应用
supervisorctl restart hyperf
# 停止 hyperf 应用
supervisorctl stop hyperf
# 查看所有被管理项目运行状态
supervisorctl status
# 重新加载配置文件
supervisorctl update
# 重新启动所有程序
supervisorctl reload