进程守护Supervisor【系列一:安装与配置】

进程守护Supervisor

准备工作:
环境:

Centos 7 (Centos6.5不适合以下教程)

Python >= 2.7 (升级见:Python升级教程)

pip包管理工具安装(已安装可跳过)

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py 

安装:

  • 安装superviosr
pip install supervisor
  • 配置配置文件

    // 创建配置目录
    mkdir -p /etc/supervisor/conf
    // 输出默认配置文件
    echo_supervisord_conf  >/etc/supervisor/supervisord.conf

    配置文件

# vim /etc/supervisor/supervisord.conf
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
;    variables can be expanded using this syntax: "%(ENV_HOME)s".
;  - Quotes around values are not supported, except in the case of
;    the environment= options as shown below.
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
;  - Command will be truncated if it looks like a config file comment, e.g.
;    "command=bash -c ‘foo ; bar‘" will truncate to "command=bash -c ‘foo ".
;
; Warning:
;  Paths throughout this example file use /tmp because it is available on most
;  systems.  You will likely need to change these to locations more appropriate
;  for your system.  Some systems periodically delete older files in /tmp.
;  Notably, if the socket file defined in the [unix_http_server] section below
;  is deleted, supervisorctl will be unable to connect to supervisord.

[unix_http_server]
file=/var/run/supervisor.sock   ; the path to the socket file
;chmod=0700                 ; socket file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)

; Security Warning:
;  The inet HTTP server is not enabled by default.  The inet HTTP server is
;  enabled by uncommenting the [inet_http_server] section below.  The inet
;  HTTP server is intended for use within a trusted environment only.  It
;  should only be bound to localhost or only accessible from within an
;  isolated, trusted network.  The inet HTTP server does not support any
;  form of encryption.  The inet HTTP server does not use authentication
;  by default (see the username= and password= options to add authentication).
;  Never expose the inet HTTP server to the public internet.

[inet_http_server]                  ; inet (TCP) server disabled by default
port=0.0.0.0:9001          ; ip_address:port specifier, *:port for all iface
; username=hadoop                     ; default is no username (open server)
; password=******                ; default is no password (open server)

[supervisord]
logfile=/var/log/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
pidfile=/var/run/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false               ; start in foreground if true; default false
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200
;umask=022                   ; process file creation umask; default 022
;user=supervisord            ; setuid to this UNIX account at startup; recommended if root
;identifier=supervisor       ; supervisord identifier, default is ‘supervisor‘
;directory=/tmp              ; default is not to cd during start
;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 rpcinterface:supervisor 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:x] sections.

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

; The supervisorctl section configures how supervisorctl will connect to
; supervisord.  configure it match the settings in either the unix_http_server
; or inet_http_server section.

[supervisorctl]
serverurl=unix:///var/run/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 in [*_http_server] if set
;password=123                ; should be same as in [*_http_server] if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The sample program section below shows all possible program subsection values.
; Create one or more ‘real‘ program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; 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)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
;exitcodes=0                   ; ‘expected‘ exit codes used with autorestart (default 0)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;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 (0 means none, default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in ‘capturemode‘ (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stdout_syslog=false           ; send stdout to syslog with process name (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=10     ; # of stderr logfile backups (0 means none, default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in ‘capturemode‘ (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;stderr_syslog=false           ; send stderr to syslog with process name (default false)
;environment=A="1",B="2"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample eventlistener section below shows all possible eventlistener
; subsection values.  Create one or more ‘real‘ eventlistener: sections to be
; able to handle event notifications sent by supervisord.

;[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)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; autorestart if exited after running (def: unexpected)
;exitcodes=0                   ; ‘expected‘ exit codes used with autorestart (default 0)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=false         ; redirect_stderr=true is not allowed for eventlisteners
;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 (0 means none, default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stdout_syslog=false           ; send stdout to syslog with process name (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=10     ; # of stderr logfile backups (0 means none, default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;stderr_syslog=false           ; send stderr to syslog with process name (default false)
;environment=A="1",B="2"       ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample group section below shows all possible group values.  Create one
; or more ‘real‘ group: sections to create "heterogeneous" process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; each refers to ‘x‘ in [program:x] definitions
;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 = /etc/supervisor/conf/*.ini
  • 配置开机启动
# vim /lib/systemd/system/supervisord.service

[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
  • 添加启动服务

    systemctl enable supervisord.service
    systemctl start/restart/stop supervisord.service
  • 增加自定义守护文件
    eg.新增服务配置文件
    # vim /etc/supervisor/conf/xdb.ini
    [program:xdb-service]
    command=/home/hadoop/anaconda3/envs/xdb/bin/python /data/service/xdb-service/app.py
    directory=/data/service/xdb-service/
    stdout_logfile=/data/service/xdb-service/log/supervisor.log
    stdout_logfile_maxbytes = 200MB
    autostart=true
    startretries=10
    user=hadoop
    systemctl reload supervisord.service

启动:

root用户启动

###启动服务

supervisorctl start all
supervisorctl start service_name

###关闭服务

supervisorctl stop all
supervisorctl stop service_name

###查看状态

supervisorctl status [service_name]

###更新配置文件

supervisorctl update

###重新启动所有服务或者是某个服务

supervisorctl restart all
supervisorctl restart service_name

原文地址:https://blog.51cto.com/leyex/2442634

时间: 2024-10-04 06:08:21

进程守护Supervisor【系列一:安装与配置】的相关文章

zabbix系列之六——安装后配置二Items

https://www.zabbix.com/documentation/3.4/manual/config/items/itemtypes/snmp 1Items 1.1creating items index detail note 1 Configuration->hosts 2 Click on Items in the row of the host 3 Click on Create item in the upper right corner of the screen 4 Ent

Mac下Supervisor进程监控管理工具的安装与配置

Supervisor是一个类 unix 操作系统下的进程监控管理工具. Supervisor是由 Python 写成,可用 Python 的包安装管理工具 pip(Python Package Index) 直接安装 代码如下: sudo pip install supervisor 配置 Supervisor Supervisor 的配置文件命名为 supervisord.conf,它为 supervisord(Supervisor 的主服务命令) 和 supervisorctl(Superv

xenserver系列之安装与配置

文章内容 1 ,安装 一.选择键盘类型:US. 二.这个是欢迎界面,选择“OK”即可. 三.这个是使用协议,我们必须得接受,否则就不能继续安装了! 四.这个是警告,告诉你XenServer所需要的硬件条件,我们选择“OK”即可. 六.这个是存储设备选择界面,第一个是本地磁盘---sda;第二个是自动精简配置---thin provisioning ,主要是针对XenDesktop MCS,如果想详细了解,可以上查查.我们这里就直接选择本地. 七.这个是安装方式选择界面,一共有三种:本地.HTTP

zabbix系列之八——安装后配置三Triggers

1Triggers(触发器) 描述 详细 备注 术语描述 1)触发器是评估监控项采集的数据的逻辑表达式,代表了当前系统状态. 2)触发器可定义一个什么数据是可接受的阈值,因此,如果接收的数据超过了可接受的状态,则触发器会被触发 - 或将状态更改为PROBLEM. 触发器状态:OK/PROBLEM 其他 如果在表达式中使用基于时间的函数(nodata(), date(), dayofmonth(), dayofweek(), time(), now()),触发器就会由Zabbix timer进程每

Redis系列2-Redis安装、配置

下载安装包 wget http://download.redis.io/releases/redis-4.0.8.tar.gz解压 tar xzvf redis-4.0.8.tar.gz安装 cd redis-4.0.8 make cd src将文件安装到指定目录 make install PREFIX=/usr/local/redis 移动配置文件.数据库文件到安装目录下 cd ../ mv redis.conf /usr/local/redis/binmv dump.rdb /usr/loc

.Net Core部署在Linux服务器:Nginx反向代理+Supervisor进程守护

前言:.Net Core 程序(网站)可以部署在windows IIS ,也可以部署在Linux系统(更加推荐) 本文部署,基于.net core 2.1,服务器CentOS 7, 需要安装的服务有2个:Nginx,SuperVisor(进程守护) 命令预览: Linux: 重启:reboot Nginx: 强杀: killall -9 nginx 测试配置是否正确: nginx -t 启动: nginx SuperVisor:停止:supervisorctl shutdown 启动: supe

Supervisor安装与配置(Linux/Unix进程管理工具)

http://blog.csdn.net/xyang81/article/details/51555473 https://www.liaoxuefeng.com/article/0013738926914703df5e93589a14c19807f0e285194fe84000 1,Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统.它可以很方便

守护进程和supervisor

守护进程和supervisor 前言 接触守护进程已经很久了,但是在工作中有时候却经常忘记它.这次是在需要后台跑一个十几万的数据处理工作,耗时较长的时候,习以为常的通过&将脚本放到后台去执行,将标准输出和错误输出重定向到日志文件.然后干其他的事情,却发现进程莫名奇妙的死掉,即使已经在所有可能出现异常的地方都比较注意,然后在合适的地方加上了try except这样的异常处理.但是任何异常信息都没有打印,后来突然发现,有可能是因为仅仅是通过&后台去跑,但是却并没有让该处理进程成为守护进程脱离终

supervisor安装和配置指南

supervisor:是用python写的一个进程管理工具,用来启动,重启,关闭进程.注意:Supervisor只能管理非daemon的进程,也就是说Supervisor不能管理守护进程.否则提示Exited too quickly (process log may have details)异常.好比Tomcat默认是以守护进程启动的,所以要改成了catalina.sh run,以前台进程的方式运行,关于守护进程和非daemon进程这里不做详细讲解,可自行百度.具体关于supervisor的相