跳到主要内容

进程守护 Supervisor

信息

Supervisor(官网) 是一个客户端/服务器系统,允许其用户在类 UNIX 操作系统上控制许多进程。

安装启动

安装

> sudo yum install -y python-pip
> sudo yum install -y supervisor

配置调整

/etc/supervisord.conf
[include]
files = /srv/config/supervisor/*.conf

安装启动

输入命令 supervisorctl 进入 supervisorctl 的 shell 交互界面

  • help # 查看帮助
  • status # 查看程序状态
  • stop program_name # 关闭 指定的程序
  • start program_name # 启动 指定的程序
  • restart program_name # 重启 指定的程序
  • tail -f program_name # 查看 该程序的日志
  • update # 重启配置文件修改过的程序(修改了配置,通过这个命令加载新的配置)

也可以直接通过 shell 命令操作:

  • supervisorctl status
  • supervisorctl update

守护配置文件指定路径:/etc/supervisor/conf.d/*.conf

新建 *.conf 文件后执行:supervisorctl update

  • 激活开机启动命令
> systemctl enable supervisord.service
  • 启动 supervisor 进程
> systemctl start supervisord.service
  • 关闭 supervisor 进程
> systemctl stop supervisord.service
  • 查看运行状态
> systemctl status supervisord.service

配置详解

[unix_http_server]
file=/var/run/supervisor.sock

[supervisord]
logfile=/var/run/supervisord.log
pidfile=/var/run/supervisord.pid

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock

; 上面这几个文件我们需要单独创建,然后赋予相应的权限即可,因为它们都在 /tmp 下面,会被清理
; 当然你也可以放在别的目录下

; 这里也必须要写上,否则无法使用 supervisorctl
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[program:my_test] ; 这里是重点,根据自身的情况进行填写
command=python3 test.py ; 被监控的进程启动命令
directory=/root/ ; 执行命令的所在目录
priority=1 ; 数字越高,优先级越高
numprocs=1 ; 启动几个进程
autostart=true ; 随着supervisord的启动而启动
autorestart=true ; 自动重启。
startretries=10 ; 启动失败时的最多重试次数
exitcodes=0 ; 正常退出代码
stopsignal=KILL ; 用来杀死进程的信号
stopwaitsecs=10 ; 发送SIGKILL前的等待时间
redirect_stderr=true ; 重定向stderr到stdout

参考文档