首页 > 技术文章 > Linux 下 supervisor 使用详解

fredy0902 2021-03-27 10:03 原文

一.supervisor 简介

Supervisor is a client/server system that allows its users to control a number of processes on UNIX-like operating systems.-----来自http://supervisord.org/introduction.html
Supervisor包含如下几个组件:
  • supervisord

    It is responsible for starting child programs at its own invocation, responding to commands from clients, restarting crashed or exited subprocesseses, logging its subprocess stdout and stderr output, and generating and handling “events” corresponding to points in subprocess lifetimes.

  • supervisorctl

    supervisor提供的一个客户端,It provides a shell-like interface to the features provided by supervisord. From supervisorctl, a user can connect to different supervisord processes (one at a time), get status on the subprocesses controlled by, stop and start subprocesses of, and get lists of running processes of a supervisord.

  • web server

    提供的一个在浏览器里可以打开的http访问的管理窗口,A (sparse) web user interface with functionality comparable to supervisorctl may be accessed via a browser if you start supervisord against an internet socket. Visit the server URL (e.g. http://localhost:9001/) to view and control process status through the web interface after activating the configuration file’s [inet_http_server] section.

 

Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。
 
二.supervisor 安装

1.配置好yum源,可以直接安装
  yum install supervisor
2.Pip安装
  pip install supervisor
 
三.supervisor 配置
1.supervisor本身的配置文件supervisord.conf,一般路径在/etc/supervisord.conf 或 /etc/supervisor/supervisord.conf,主要分为以下几个重要部分
  • [unix_http_server]  ;UNIX socket 文件,supervisorctl 会使用
  • [inet_http_server]  ;HTTP服务器,提供web管理界面
  • [supervisord]  ;日志文件,默认是 $CWD/supervisord.log
  • [supervisorctl]  ;通过UNIX socket连接supervisord,包含一些设置与supervisorctl交互的shell命令
  • [include]  管理子程序的配置文件路径

2.supervisor管理的子程序的配置文件xxx.ini,一般路径在/etc/supervisor/config.d/

此文件以 [program:xx(program name)] 开头,xx是supervisor要监控的子程序的名字,

 

四.supervisor 命令

  1.supervisord的启动  ;要supervisor监控子程序,必须先把supervisord起来  

    supervisord -c /etc/supervisor/supervisord.conf

  2.supervisorctl  ;路径/usr/bin/supervisorctl

  •  supervisorctl status ;查看所有进程状态
  •  supervisorctl reload ;重启配置文件里的所有程序
  •  supervisorctl  update ;重载配置文件的改动,并重启相应的进程
  •     supervisorctl  start proc1 ;启动程序proc1
  •     supervisorctl  start all ;启动所有程序
  •     supervisorctl  pid ;查看supervisord 进程ID

  直接输入supervisorctl进入supervisorctl的shell交互界面,此时上面的命令不带supervisorctl可直接使用。

更详细介绍,请参照supervisor官网:http://supervisord.org/

推荐阅读