Supervisor是python2写就的一款强大的运维工具(其实如今已经支持Python3了 https://github.com/Supervisor/supervisor)
那么怎么利用Supervisor监控python3程序呢?本文主要讲述Supervisor在CentOS下的安装部署。html
安装及设置
可经过pip3安装,若是你已是python3的pip3,能够用一下命令安装python
pip3 install git+https://github.com/Supervisor/supervisor
若是是python2,能够用CentOS (系统自带Python2)的yum安装git
sudo yum install supervisor
运行echo_supervisord_conf > /etc/supervisor/supervisord.conf
来产生设置,未避免产生非root用户的权限错误,将/etc/supervisor/supervisord.conf
内[unix_http_server]
这项改成 (修改chmod):github
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
chmod=0766 ; 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))
再将末尾的[include]
部分改成:web
[include]
files = /etc/supervisor/*.conf ;若是后面启动出错,“说已经include path了”,把这行删掉
files = /etc/supervisor/conf.d/*.conf
这样方便为每一个app单独设置conf文件而没必要所有写在全局设置里面。
在启动supervisorctl
须先启动supervisord
,不然会出现error: <class 'socket.error'>, [Errno 99] Cannot assign requested address: file: /usr/lib/python/socket.py line: 575
错误:shell
1 sudo supervisord -c /etc/supervisor/supervisord.conf (先启动supervisord)
2 sudo supervisorctl -c /etc/supervisor/supervisord.conf
若是 supervisord 出错:app
Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
For help, use /bin/supervisord -h
ps -ef | grep supervisord
You will get some pid of supervisord just like thesesocket
root 2641 12938 0 04:52 pts/1 00:00:00 grep --color=auto supervisord
root 29646 1 0 04:45 ? 00:00:00 /usr/bin/python /usr/local/bin/supervisord
if you get output like that, your pid is the second one. then if you want to shut down your supervisord you can do this工具
hope it helpful. ref: http://supervisord.org/running.html#signals
在/etc/supervisor/conf.d/
里新建app.conf
文件(这是咱们本身的conf,用来启停本身的程序),
1 [program:awesome]
2 environment=PYTHONPATH='/home/username/.local/lib/python3.6/site-packages/' ;这行很是重要,后面运行的时候,若是出错,说找不到package, 须要在这里指定package的path
3 command = /usr/bin/python3 /srv/awesome/www/app.py
4 directory = /srv/awesome/www
5 user = yshi2
6 startsecs = 3
7
8 redirect_stderr = true
9 stdout_logfile_maxbytes = 50MB
10 stdout_logfile_backups = 10
11 stdout_logfile = /srv/awesome/log/app.log
其余的例子:
1 [program:app]
2 directory = ~/su/ ; 程序的启动目录
3 command = /home/hadoop/anaconda3/bin/python /home/hadoop/su/app.py ; 启动命令,能够看出与手动在命令行启动的命令是同样的,注意这里home不可用~代替
4 autostart = true ; 在 supervisord 启动的时候也自动启动
5 startsecs = 5 ; 启动 5 秒后没有异常退出,就看成已经正常启动了
6 autorestart = true ; 程序异常退出后自动重启
7 startretries = 3 ; 启动失败自动重试次数,默认是 3
8 user = hadoop ; 用哪一个用户启动
9 redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false
10 stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB
11 stdout_logfile_backups = 20 ; stdout 日志文件备份数
12 ; stdout 日志文件,须要注意当指定目录不存在时没法正常启动,因此须要手动建立目录(supervisord 会自动建立日志文件)
13 stdout_logfile = /tmp/app.log
再介绍两个有用的配置项stopasgroup
和killasgroup
,若是咱们用Flask等Rest服务,一般其会开启几个进程,那么若是stopasgroup
不启用的话,supervisor没法重启此服务(关闭主进程时其子进程没有关闭,再开启主进程时会提示端口被占用等错误信息)。
1 ; 默认为 false,若是设置为 true,当进程收到 stop 信号时,会自动将该信号发给该进程的子进程。若是这个配置项为 true,那么也隐含 killasgroup 为 true。例如在 Debug 模式使用 Flask 时,Flask 不会将接收到的 stop 信号也传递给它的子进程,所以就须要设置这个配置项。
2 stopasgroup=false ; send stop signal to the UNIX process
3 ; 默认为 false,若是设置为 true,当进程收到 kill 信号时,会自动将该信号发给该进程的子进程。若是这个程序使用了 python 的 multiprocessing 时,就能自动中止它的子线程。
4 killasgroup=false ; SIGKILL the UNIX process group (def false)
这里咱们能够看出,虽然supervisor是python2写的,但只要咱们指定运行的python3解释器去运行程序就好了。
运行supervisorctl
,便可在shell里面方便的操做,如start app
、restart app
等。
若须要web界面,可在/etc/supervisor/supervisord.conf
内修改,
1 [inet_http_server] ; inet (TCP) server disabled by default
2 port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface, 若的形式*:port则开放外网访问 )
3 ;username=user ; (default is no username (open server))
4 ;password=123 ; (default is no password (open server))
重启supervisorctl
后便可在127.0.0.1:9001
见到web界面,

注意事项
- 若是修改了 /etc/supervisord.conf ,须要执行 supervisorctl reload 来从新加载配置文件,不然不会生效。。。
- 不少时候用supervisor管理后台进程容易失败,如
hbase/bin/hbase-daemon.sh start thrift
,这时候能够改用前台进程如/usr/local/hbase/bin/hbase thrift start
。
- 可让supervisord service 随机启动
systemctl enable supervisord
systemctl restart supervisord