Supervisor-守护进程工具

说明

Supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

安装

安装方式

  1. 用python安装工具安装
# 安装Python包管理工具
yum install python-setuptools# 安装Supervisor
easy_install supervisor
  1. 用Pip工具安装
pip install supervisor

检查是否安装成功

使用命令:

echo_supervisord_conf

有输出配置信息即是安装成功

使用

初始化主程序配置

# 创建目录存放配置文件(自定义目录)
mkdir /etc/supervisor# 写入主程序配置
echo_supervisord_conf > /etc/supervisor/supervisord.conf

配置说明

; Sample supervisor config file.[unix_http_server]
file=/var/run/supervisor/supervisor.sock   ; socket 路径;chmod=0700                 ; socket 文件的权限
;chown=nobody:nogroup       ; socket 所属用户及组
;username=user              ; 用户名
;password=123               ; 密码;[inet_http_server]         ; 是否启用服务,默认是关闭的(启用的话可以看到supervisor 管理的服务状态)
;port=127.0.0.1:9001        ; 监听的IP及端口
;username=user              ; 用户名
;password=123               ; 密码[supervisord]               ; supervisord 全局配置
logfile=/var/log/supervisor/supervisord.log  ; supervisor 日志路径
logfile_maxbytes=50MB       ; 单个日志文件最大数
logfile_backups=10          ; 保留多少个日志文件(默认10个)
loglevel=info               ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/supervisord.pid ; pid 文件路径
nodaemon=false              ; 启动是否丢到前台,设置为false ,表示以daemon 的方式启动
minfds=1024                 ; 最小文件打开数,对应系统limit.conf 中的nofile ,默认最小为1024,最大为4096
minprocs=200                ; 最小的进程打开数,对应系统的limit.conf 中的nproc,默认为200
;umask=022                  ; (process file creation umask;default 022)
;user=chrism                 ; 启动supervisord 服务的用户,默认为root
;identifier=supervisor       ; (supervisord identifier, default is 'supervisor')
;directory=/tmp              ; 这里的目录指的是服务的工作目录
;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value       ; (key value pairs to add to environment)
;strip_ansi=false            ; (strip ansi escape codes in logs; def. false); the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface[supervisorctl]
serverurl=unix:///var/run/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.;[program:theprogramname]      ; 定义一个守护进程 ,比如下面的elasticsearch
;command=/bin/cat              ; 启动程序使用的命令,可以是绝对路径或者相对路径
;process_name=%(program_name)s ; 一个python字符串表达式,用来表示supervisor进程启动的这个的名称,默认值是%(program_name)s
;numprocs=1                    ; Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须包含%(process_num)s,默认是1
;directory=/tmp                ; supervisord在生成子进程的时候会切换到该目录
;umask=022                     ; umask for process (default None)
;priority=999                  ; 权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999
;autostart=true                ; 如果设置为true,当supervisord启动的时候,进程会自动启动
;autorestart=true              ; 设置为随 supervisord 重启而重启,值可以是false、true、unexpected。false:进程不会自动重启
;startsecs=10                  ; 程序启动后等待多长时间后才认为程序启动成功,默认是10秒
;startretries=3                ; supervisord尝试启动一个程序时尝试的次数。默认是3
;exitcodes=0,2                 ; 一个预期的退出返回码,默认是0,2。
;stopsignal=QUIT               ; 当收到stop请求的时候,发送信号给程序,默认是TERM信号,也可以是 HUP, INT, QUIT, KILL, USR1, or USR2
;stopwaitsecs=10               ; 在操作系统给supervisord发送SIGCHILD信号时等待的时间
;user=chrism                   ; 如果supervisord以root运行,则会使用这个设置用户启动子程序
;redirect_stderr=true          ; 如果设置为true,进程则会把标准错误输出到supervisord后台的标准输出文件描述符
;stdout_logfile=/a/path        ; 把进程的标准输出写入文件中,如果stdout_logfile没有设置或者设置为AUTO,则supervisor会自动选择一个文件位置
;stdout_logfile_maxbytes=1MB   ; 标准输出log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小
;stdout_logfile_backups=10     ; 标准输出日志轮转备份的数量,默认是10,如果设置为0,则不备份
;stdout_capture_maxbytes=1MB   ; 当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB
;stdout_events_enabled=false   ; 如果设置为true,当进程在写它的stderr
;stderr_logfile=/a/path        ; 把进程的错误日志输出一个文件中,除非redirect_stderr参数被设置为true
;stderr_logfile_maxbytes=1MB   ; 错误log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小
;stderr_logfile_backups=10     ; 错误日志轮转备份的数量,默认是10,如果设置为0,则不备份
;stderr_capture_maxbytes=1MB   ; 当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB
;stderr_events_enabled=false   ; 如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发
;environment=A=1,B=2           ; 一个k/v对的list列表
;serverurl=AUTO                ; 是否允许子进程和内部的HTTP服务通讯,如果设置为AUTO,supervisor会自动的构造一个url; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.#这个地方是自定义一个守护进程
[program:elasticsearch]                       ; 定义一个守护进程 elasticsearch
environment=ES_HOME=/usr/local/elasticsearch  ; 设置ES_HOME 环境变量
user=elk                                      ; 启动elasticsearch 的用户
directory=/usr/local/elasticsearch            ; 进入到这个目录中
command=/usr/local/elasticsearch/bin/elasticsearch ; 执行启动命令
numprocs=1                                    ; Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须包含%(process_num)s,默认是1
autostart=true                                ; 设置为随 supervisord 启动而启动
autorestart=true                              ; 设置为随 supervisord 重启而重启
startretries=3                                ; 设置elasticsearch 重启的重试次数
priority=1                                    ; 权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999  ;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=unexpected        ; restart at unexpected quit (default: unexpected)
;startsecs=10                  ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups        ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=1,B=2           ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils); The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.;[group:thegroupname]          ; 服务组管理,可以将多个服务名写到这里管理(组名自定义)
;programs=progname1,progname2  ; 上面配置好的服务名,比如elasticsearch,kibana,logstash
;priority=999                  ; the relative start priority (default 999); The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.[include]
files = supervisord.d/*.ini

子程序配置

  1. 为了方便维护配置,一般子程序配置不与主程序配置放一起,在主配置文件目录下创建文件夹:
mkdir supervisord.d
  1. 并修改主配置文件内[include]下的files = supervisord.d/*.ini,改为:
[include]
files = supervisord.d/*.conf

当然这步可以不改,改动只是为了和主配置文件后缀一致。

  1. 创建子程序配置文件

例如:创建test.conf并写入配置内容:

[program:TestServer]                            ; 项目名(启动子进程时需要用到)
command=/usr/bin/php /var/www/html/test.php     ; 脚本执行命令
directory=/var/www/html                            ; 脚本启动命令
autostart=true                                 ; supervisor启动的时候是否随着同时启动,默认True
autorestart=true                               ; 当程序exit的时候,这个program不会自动重启,默认unexpected,设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。如果为false的时候,无论什么情况下,都不会被重新启动,如果为unexpected,只有当进程的退出码不在下面的exitcodes里面定义的
startsecs=3                                        ; 这个选项是子进程启动多少秒之后,此时状态如果是running,则我们认为启动成功了。默认值为1
startretries=3                                 ; 启动失败时的最多重试次数
stderr_logfile=/var/www/html/logs/test.err.log ; stderr日志文件存放目录
stdout_logfile=/var/www/html/logs/test.out.log ; stderr日志文件存放目录
stderr_logfile_maxbytes=2MB                        ; stderr日志文件大小,默认 50MB
stdout_logfile_maxbytes=2MB                        ; stdout日志文件大小,默认 50MB
user=root                                      ; 脚本运行的用户身份
priority=999                                   ; 数字越高,优先级越高
numprocs=1                                     ; 启动几个进程
stopsignal=KILL                                    ; 用来杀死进程的信号
stopwaitsecs=10                                    ; 发送SIGKILL前的等待时间

启动程序

# 启动主程序
# 如果主配置设置了inet_http_server,主程序运行后可根据配置的ip和端口打开Web界面操作
supervisord -c /etc/supervisor/supervisord.conf### 主程序应用操作# 重启所有应用(注:start、restart、stop都不会载入最新的配置文)
supervisorctl restart all
# 停止所有应用
supervisorctl stop all
# 启动所有应用
supervisorctl start all
# 查看当前运行的进程列表
supervisorctl status
# 更新新的配置到supervisord(不会重启原来已运行的程序)
supervisorctl update
# 载入所有配置文件,并按新的配置启动、管理所有进程(会重启原来已运行的程序)
supervisorctl reload
# 当一个服务由自动启动修改为手动启动时执行一下就ok
supervisorctl reread

按照以上的配置,主程序启动时子程序也会顺带启动。当然也可以手动操作子程序的服务状态。

# 重启指定应用
supervisorctl restart TestServer
# 停止指定应用
supervisorctl stop TestServer
# 启动指定应用
supervisorctl start TestServer

常见问题

  1. unix:///var/run/supervisor.sock no such file

问题描述:安装好supervisor没有开启服务直接使用supervisorctl报的错

解决方案:执行supervisord -c /etc/supervisor/supervisord.conf

  1. Unlinking stale socket /tmp/supervisor.sock

问题描述:启动时提示,可能是主程序被意外终止导致。

解决方案:执行unlink /tmp/supervisor.sock

配置为自启动服务

  1. 新建服务文件
# 切换到系统服务文件目录
cd /usr/lib/systemd/system/# 新建服务文件
vim supervisord.service
  1. 写入服务内容
[Unit]
Description=Supervisor daemon[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s[Install]
WantedBy=multi-user.target
  1. 设置为自启动
systemctl enable supervisord

守护进程 - Supervisor的使用相关推荐

  1. linux 安装守护进程supervisor

    linux的守护进程类似于windows的服务.linux通过supervisor创建守护进程. 1.安装supervisor sudo apt-get install supervisor -- u ...

  2. laravel 开启redis队列 守护进程 supervisor

    1.使用redis作为队列,修改config的queue配置,修改.nev QUEUE_CONNECTION = redis 2.创建执行队列的工作类,php artisan make:job Tes ...

  3. 搭建Jenkins+Github的自动部署环境并使用守护进程(Supervisor)部署Golang(gin)项目

    零.前言 Jenkins是一个自动部署的平台,可以极大的帮助我们自动部署项目,把注意力放在书写代码上. 教程环境说明: 系统:ubuntu20 配置:2核4G 软件:JAVA11 安装方式:APT(不 ...

  4. 守护进程: supervisor使用

    在日常的工作中,通过频繁的登录机器来管理任务是一件比较麻烦的事情,另外程序也有可能因为以为偶然因素停止运行,这种情况我们希望可以让它们自动重启,supervisor(官网)是一个unix的系统进程管理 ...

  5. ASP.NET Core Linux下为 dotnet 创建守护进程(必备知识)

    前言 在上篇文章中<ASP.NET Core Docker部署>中介绍了如何在 Docker 容器中部署我们的 asp.net core 应用程序,本篇主要是怎么样为我们在 Linux 或 ...

  6. Linux supervisor守护进程的安装和使用

    个人网站:http://xiaocaoshare.com/ supervisor守护进程的介绍 Supervisor(http://supervisord.org/)是用Python开发的一个clie ...

  7. supervisor 守护php,laravel队列之Supervisor守护进程(centos篇)

    前言 unbutun和centos均为linux系统,官方为我们提供了Homestead可以很好的模拟在服务器中的开发和运行.之前写了一篇基于Homestead(unbutun)关于Superviso ...

  8. linux 守护进程管理 supervisor 简介 可用于docker容器内守护进程

    一.supervisor简介 Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启.它是通过fork/e ...

  9. supervisor 守护多个进程_supervisor管理守护进程

    在Linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件. 由于在linux中,每个系统与用户进行交流的 ...

最新文章

  1. 策略模式(封装一系列的功能,使之可以相互替换)
  2. Python 面向对象封装和继承
  3. php获取数据库不同,php – 从master数据库获取数据库凭据,然后连接到不同的数据库...
  4. Java8 LinkedHashMap 源码阅读
  5. 12.15模拟:总结
  6. 简易 Vue 构建--篇三
  7. 天体运行轨迹_都说运动是天体的基本特性,那么银河系是绕着什么运动的呢?...
  8. 如何开发一个油猴脚本- 从零开始编写一个油猴脚本
  9. 没有找到MSVCR100.dll解决方法
  10. 通讯录管理系统的设计与实现
  11. 腾讯视频qlv格式怎么转换成mp4在手机上播放
  12. WinXP去掉桌面图标阴影
  13. vdi linux桌面,ubuntu VDI ( Ulteo Open Virtual Desktop
  14. python里str什么意思_python中str是什么意思
  15. 基于微信小程序奶茶店在线点单管理系统#毕业设计
  16. 讲解后台管理系统之列表设计分享
  17. MySQL基础语法大全(尚硅谷)
  18. std::tuple、std::tie(可用于结构体大小比较)、std::pair用法
  19. 艾为aw87359 PA 关机状态下会漏电
  20. 高考生如何用心理暗示给自己减压?

热门文章

  1. c语言面试题东软,【东软集团程序员Java东软集团C语言面试题】面试问题:东软java面… - 看准网...
  2. sqlserver查询时间范围
  3. 图解http(七)-web的攻击技术
  4. Mobile net系列总结(V1、V2、V3)
  5. WeCenter 与 UCenter 对接
  6. Mac安装photoshop错误:安装程序未能初始化
  7. 数据结构(Data Structure)(C/C++)PTA习题+课后习题
  8. android_图片转视频_image2video
  9. 考研面试常问问题汇总(包含答案)
  10. inet_aton和inet_ntoa