应用背景
本项目在v0.1版本出来后,需要搭载在VPS上,但是问题在于此VPS不是一个人在用,存在别人不小心中断该进程的情况。所以为了防止这种以及其他意外中断的情况,在师兄的建议下,使用了supervisor这个软件。
软件介绍
supervisor是用python编写的,在之前我也想自己简单写一个适用于本项目的类似软件。师兄觉得时间可能会多一点,且没必要,现在看来,中间的坑浪费的时间足有两天,已经够我自己写了,但碰到坑,我总会扛到底,很是无奈。
supervisor由两部分构成(C/S结构),一个是server端的supervisord,另一个则是client端的supervisorctl。总体的配置是由supervisord来进行,具体的某个进程由supervisorctl进行单门的启动和关闭。
在本项目中的应用
由于本项目的要求即是自动运行某进程,使其在意外中断的情况进行重启,扮演守护进程的角色,所以只用到两个功能:
1.运行。
2.输出日志
配置环境
#
uname -a
Linux 2.6.32-042stab128.2 #1 SMP Thu Mar 22 10:58:36 MSK 2018 x86_64 x86_64 x86_64 GNU/Linux
root@JAW8:/etc# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
安装过程
我试过多种安装,主要分两种:
1.安装工具pip或apt-get或easy_install来安装
pip install supervisor
或者
easy_install supervisor
或者
apt-get install supervisor
2.通过下载安装
我安装成功的是用了pip,注意是python2版本
配置过程
主要分两个步骤,第一个是修改整体配置文件,第二个则是创建单个进程的配置
第一步:修改并配置整体supervisord.conf
这里有两个坑,一定要注意
第一个:
需用到一条命令,这个是pip安装方式所特有的:
echo_supervisor > /etc/supervisord.conf
这样整体的配置文件就生成了,里面的每个模块,每个参数的具体意义,建议参照官方文档,别乱看教程
这是官方链接:http://supervisord.org/configuration.html
坑来了:
里面的/tmp目录都修改了,我的改法是:
[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)
;[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=/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=chrism ; default is current user, required 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
[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=你自己的用户名 ; should be same as in [*_http_server] if set
password=自己的密码 ; 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,2 ; 'expected' exit codes used with autorestart (default 0,2)
;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)
;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)
;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,2 ; 'expected' exit codes used with autorestart (default 0,2)
;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)
;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)
;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
files = /etc/supervisor/*.conf
留意这些目录,一定要保证存在,没有就自己建。开始适用时,可以直接用
supervisor -c /etc/supervisord.conf
来初步运行,-c是为了制定目录,你可以换目录存放,但一定要一致。
如果报已运行,或sock无法连接等,一般是初步运行的supervisor没有停止所以需要查看进程并将其关闭,具体方法如下:
ps -aux|grep supervisor
root 17177 0.0 0.3 54020 7048 ? Ss 10:09 0:00 /usr/bin/python /usr/local/bin/supervisord -c /etc/supervisord.conf
root 17688 0.0 0.0 8820 780 pts/0 S+ 11:16 0:00 grep --color=auto supervisor
最后一个不需要关,其他的都关掉
kill -9 PID
每次不行就可以尝试这些,反正就是关了开,配下,不行,再关,行了又想修改,再关。。
我之前在这些问题上纠结了很久
第二个坑:单个配置文件的加载目录由整体配置文件的最后一行决定,即
[include]
;files = relative/directory/*.ini
files = /etc/supervisor/*.conf
一定要将【include】前边的注释符去掉,这个把我整疯了
第二步:修改单个的配置
单个配置:
官方有,但我也给个自己的吧,不需要很复杂,具有自启动和输出日志即可
#名字
[program:进程名]
directory=项目路径
command=/usr/bin/python3 /项目路径/程序.py
#supervisor
autostart=true
autorestart=true
startsecs=1
stderr_logfile=/run/Fanucpot/tmp_log/blog_stderr.log
stdout_logfile=/run/Fanucpot/tmp_log/blog_stdout.log
;user = 用户名(此处不用,注释了)
redirect_stderr = true
;stdout_logfile_maxbytes = 20M
;stdout_logfile_backups = 20
注意:不需要的就直接用;注释掉
收工
下来就是每个进程配一个配置,打开supervisorctl,有一些简单的命令,开启,关闭,查看单个进程的状态,此处不赘述
supervisorctl
即可进入客户端,做你想做的事情。
网友评论