[关闭]
@dungan 2020-11-03T00:53:00.000000Z 字数 1519 阅读 92

Linux

Supervisor


Supervisor 是一个 C/S 模型的程序,supervisord 是 server 端,supervisorctl 是 client 端。

通过 Supervisor 管理的进程,当进程意外被 Kill 时,Supervisor 会自动将它重启,可以很方便地做到进程自动恢复的目的,而无需自己编写 shell 脚本来管理进程。

1. 安装 Supervisor

  1. yum install y epelrelease
  2. yum install y supervisor

2. Supervisor 配置文件

supervisord 的配置文件默认位于 /etc/supervisord.conf

  1. ; supervisor config file
  2. ...
  3. ...
  4. ; include 项加载的是需要管理的进程的配置文件
  5. [include]
  6. files = /etc/supervisor/conf.d/*.conf;

3. program 配置文件

上一步Supervisor的配置文件中我们指定/etc/supervisor/conf.d/*.conf 为被管理进程的配置文件,内容如下:

  1. # 新建一个应用并设置一个名称,这里设置为 hyperf
  2. [program:hyperf]
  3. # 设置命令在指定的目录内执行
  4. directory=/var/www/hyperf/
  5. # 设置环境变量
  6. environment=PATH="/home/app_env/bin"
  7. # 这里为您要管理的项目的启动命令
  8. command=php ./bin/hyperf.php start
  9. # 以哪个用户来运行该进程
  10. user=root
  11. # supervisor 启动时自动该应用
  12. autostart=true
  13. # 进程退出后自动重启进程
  14. autorestart=true
  15. #把 stderr 重定向到 stdout,默认 false
  16. redirect_stderr=true
  17. # 进程持续运行多久才认为是启动成功
  18. startsecs=1
  19. # 重试次数
  20. startretries=3
  21. # stderr 日志输出位置,标准错误日志
  22. stderr_logfile=/var/www/hyperf/runtime/stderr.log
  23. # stdout 日志输出位置,标准输出日志
  24. stdout_logfile=/var/www/hyperf/runtime/stdout.log

4. supervisord 启动

配置文件修改完毕后,就可以启动 supervisor 服务了。

  1. supervisord c /etc/supervisord.conf

当用supervisor监护进程时,被监护进程不能是守护进程,这是由于守护进程通常会在fork完子进程后就让父进程”结束生命”,也即由supervisor创建的父进程退出,此时,supervisor无法再监护已退出进程创建出来的子进程。

5. supervisorctl 管理项目

supervisorctl 是 supervisord 的命令行客户端工具,使用的配置和 supervisord 一样,supervisorctl 进入 supervisorctl 的 shell 交互界面,也可以直接通过 shell 命令操作。

  1. # 启动 hyperf 应用
  2. supervisorctl start hyperf
  3. # 重启 hyperf 应用
  4. supervisorctl restart hyperf
  5. # 停止 hyperf 应用
  6. supervisorctl stop hyperf
  7. # 查看所有被管理项目运行状态
  8. supervisorctl status
  9. # 重新加载配置文件
  10. supervisorctl update
  11. # 重新启动所有程序
  12. supervisorctl reload

参考

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注