supervisor安装和命令介绍
一、安装
下载最新的supervisor安装包:https://pypi.python.org/pypi/supervisor
cd /usr/local/src
wget https://pypi.python.org/packages/7b/17/88adf8cb25f80e2bc0d18e094fcd7ab300632ea00b601cbbbb84c2419eae/supervisor-3.3.2.tar.gz
tar -zxvf supervisor-3.3.2.tar.gz
cd supervisor-3.3.2
python setup.py install
Tips:错误1:ImportError: No module named setuptools
## 下载pip
wget https://files.pythonhosted.org/packages/c2/f7/c7b501b783e5a74cf1768bc174ee4fb0a8a6ee5af6afa92274ff964703e0/setuptools-40.8.0.zip
unzip setuptools-40.8.0.zip
cd setuptools-40.8.0
python setup.py install
Tips:错误2:error: Could not find suitable distribution for Requirement.parse(‘meld3>=0.6.5‘),安装meld扩展
wget https://pypi.python.org/packages/45/a0/317c6422b26c12fe0161e936fc35f36552069ba8e6f7ecbd99bbffe32a5f/meld3-1.0.2.tar.gz#md5=3ccc78cd79cffd63a751ad7684c02c91
tar zxvf meld3-1.0.2.tar.gz
cd meld3-1.0.2
python setup.py install
Tips:本地python版本为python3以下
二、配置
1.生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
2.启动
supervisord -c /etc/supervisord.conf
##查看 supervisord 是否在运行:
ps aux | grep supervisord
3.基础配置
(1)打开配置文件
vim /etc/supervisord.conf
(2)在配置文件底部,配置include
[include]
files=/etc/supervisor/*.conf #若你本地无/etc/supervisor目录,请自建
(3)用supervisor管理进程,配置如下:
cd /etc/supervisor
vim ossfs.conf # 这里的文件名称自定义
(4)加入以下内容:
; 设置进程的名称,使用 supervisorctl 来管理进程时需要使用该进程名
[program:your_program_name]
command=python server.py --port=9000
;numprocs=1 ; 默认为1
;process_name=%(program_name)s ; 默认为 %(program_name)s,即 [program:x] 中的 x
directory=/home/python/tornado_server ; 执行 command 之前,先切换到工作目录
user=oxygen ; 使用 oxygen 用户来启动该进程
; 程序崩溃时自动重启,重启次数是有限制的,默认为3次
autorestart=true
redirect_stderr=true ; 重定向输出的日志
stdout_logfile = /var/log/supervisord/tornado_server.log
loglevel=info
(5)这里是启动要配置的参数,请根据自己的项目自定义添加
更改了supervisor配置文件,需要重启,运行以下指令:
supervisorctl reload
(6)服务启动
supervisord -c /etc/supervisord.conf
(7)用浏览器来管理进程配置
打开配置文件
vim /etc/supervisord.conf
配置 inet_http_server
[inet_http_server]
port=127.0.0.1:9001; 服务器ip
username=username ;自定义
password=password ;自定义
三、supervisorctl的用法
supervisord : 启动supervisor
supervisorctl reload :修改完配置文件后重新启动supervisor
supervisorctl status :查看supervisor监管的进程状态
supervisorctl start all | 进程名 :启动全部或某进程
supervisorctl stop all | 进程名 :停止全部或某进程
supervisorctl stop all:停止进程,注:start、restart、stop都不会载入最新的配置文件。
supervisorctl update:根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
四、设置开机启动
(1)添加开机脚本
vim /etc/init.d/supervisord
脚本如下:
#! /bin/sh
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO
# Author: Dan MacKinlay <danielm@phm.gov.au>
# Based on instructions by Bertrand Mathieu
# http://zebert.blogspot.com/2009/05/installing-django-solr-varnish-and.html
# Do NOT "set -e"
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
DESC="Description of the service"
NAME=supervisord
DAEMON=/usr/local/bin/supervisord
DAEMON_ARGS=" -c /etc/supervisord.conf"
#PIDFILE=/var/run/$NAME.pid
PIDFILE=/tmp/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS \
|| return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
:
(2)设置该脚本为可以执行
sudo chmod +x /etc/init.d/supervisord
(3)设置为开机自动运行
sudo update-rc.d supervisord defaults
(4)试一下,是否工作正常
service supervisord stop
service supervisord start
若报错:insserv: warning: script ‘service’ missing LSB tags and overrides,请执行:
sudo apt-get remove insserv
五、错误排查
1、unix:///var/run/supervisor/supervisor.sock no such file
问题描述:安装好supervisor没有开启服务直接使用supervisorctl报的错
解决办法:启动服务
supervisord -c /etc/supervisord.conf
2、启动了多个supervisord服务,导致无法正常关闭服务
问题描述:多次运行supervisord -c /etc/supervisord.conf 或supervisord,导致有些进程被多个superviord管理,无法正常关闭进程。
解决办法:kill相关的进程。
ps -fe | grep supervisord #查看所有启动过的supervisord服务
kill -9 PID #kill相关进程
3、supervisord 启动失败 Error: Another program is already listening on a port that one of our HTTP serve…
unlink /var/run/supervisor.sock
unlink /tmp/supervisor.sock
这个错误的原因就是supervisor.sock 这个文件会被系统自动删除或者其它原因不存在了,删除软连接就可以了。 supervisor.sock 生成的位置可以去 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=/tmp/supervisor.sock ; the path to the socket file
file=/home/management/SAVA/NetworkManageSystem/supervisor/supervisor.sock
;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=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
[supervisord]
logfile=/tmp/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=/home/management/SAVA/NetworkManageSystem/supervisor/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
silent=false ; no logs to stdout 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:///home/management/SAVA/NetworkManageSystem/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 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 = relative/directory/*.ini
[program:daphne]
socket=tcp://0.0.0.0:10083
directory=/home/management/SAVA/NetworkManageSystem
command=daphne -b 0.0.0.0 -p 10083 --proxy-headers NetworkManageSystem.asgi:application
numprocs=1
process_name=asgi%(process_num)d
autostart=true
autorestart=true
stdout_logfile=/tmp/asgi.log
redirect_stderr=true
[program:uwsgi]
command=uwsgi --ini uwsgi.ini
directory=/home/management/SAVA/NetworkManageSystem
autostart=true
autorestart=true
stdout_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/uwsgi_out.log
stderr_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/uwsgi_err.log
redirect_stderr=true
stopasgroup=true
killasgroup=true
[program:celeryworker]
command=celery -A NetworkManageSystem worker -l info
directory=/home/management/SAVA/NetworkManageSystem
numprocs=1
stdout_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/celeryworker_out.log
stderr_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/celeryworker_err.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=600
priority=15
[program:celerybeat]
command=celery -A NetworkManageSystem beat -l info
directory=/home/management/SAVA/NetworkManageSystem
numprocs=1
stdout_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/celerybeat_out.log
stderr_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/celerybeat_err.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=600
priority=15
[program:nginx]
command=/usr/local/nginx/sbin/nginx -g 'daemon off;'
autostart=true
autorestart=true
stdout_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/nginx_out.log
stderr_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/nginx_err.log
[program:redis]
command=/usr/local/bin/redis-server
autostart=true
autorestart=true
stdout_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/redis_out.log
stderr_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/redis_err.log
[program:log_server]
command=python3 server/log_server.py
directory=/home/management/SAVA/NetworkManageSystem
autostart=true
autorestart=true
stdout_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/log_server_out.log
stderr_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/log_server_err.log
;In case that kafka and zk are deployed on other servers, make sure they are supervised.
[program:zookeeper]
command=bash -c './bin/zookeeper-server-start.sh ./config/zookeeper.properties'
directory=/home/management/SAVA/kafka/kafka_2.13-3.2.0
autostart=true
autorestart=true
stdout_logfile=/home/management/SAVA/omc/SavaC/supervisor/zookeeper_out.log
stderr_logfile=/home/management/SAVA/omc/SavaC/supervisor/zookeeper_err.log
priority=500
[program:kafka]
command=bash -c './bin/kafka-server-start.sh ./config/server.properties'
directory=/home/management/SAVA/kafka/kafka_2.13-3.2.0
autostart=true
autorestart=true
stdout_logfile=/home/management/SAVA/omc/SavaC/supervisor/kafka_out.log
stderr_logfile=/home/management/SAVA/omc/SavaC/supervisor/kafka_err.log
priority=700
[program:celeryworkerC]
command=celery -A SavaC worker -l debug
directory=/home/management/SAVA/omc/SavaC
numprocs=1
stdout_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/celeryworkerC_out.log
stderr_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/celeryworkerC_err.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=600
priority=15
[program:consumer]
command=python3 manage.py kafka_consume
directory=/home/management/SAVA/omc/SavaC
numprocs=1
stdout_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/consume_out.log
stderr_logfile=/home/management/SAVA/NetworkManageSystem/supervisor/consume_err.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=600
priority=15