supervisor python_使用supervisor支持Python3程序 (解决找不到Module的问题)

Supervisor是python2写就的一款强大的运维工具(其实现在已经支持Python3了 https://github.com/Supervisor/supervisor)

那么怎么利用Supervisor监控python3程序呢?本文主要讲述Supervisor在CentOS下的安装部署。

安装及设置

可通过pip3安装,如果你已经是python3的pip3,可以用一下命令安装

pip3 install git+https://github.com/Supervisor/supervisor

如果是python2,可以用CentOS (系统自带Python2)的yum安装

sudo yum install supervisor

运行echo_supervisord_conf > /etc/supervisor/supervisord.conf来产生设置,未避免产生非root用户的权限错误,将/etc/supervisor/supervisord.conf内[unix_http_server]这项改为 (修改chmod):

[unix_http_server]file=/tmp/supervisor.sock ; (the path to the socket file)chmod=0766 ; socket file mode (default 0700)

;chown=nobody:nogroup ; socket fileuid:gid owner

;username=user ; (default is no username (open server))

;password=123 ; (default is no password (open server))

再将末尾的[include]部分改为:

[include]

files= /etc/supervisor/*.conf ;如果后面启动出错,“说已经include path了”,把这行删掉

files = /etc/supervisor/conf.d/*.conf

这样方便为每个app单独设置conf文件而不必全部写在全局设置里面。

在启动supervisorctl须先启动supervisord,否则会出现error: , [Errno 99] Cannot assign requested address: file: /usr/lib/python/socket.py line: 575错误:

1 sudo supervisord -c /etc/supervisor/supervisord.conf (先启动supervisord)2 sudo supervisorctl -c /etc/supervisor/supervisord.conf

如果 supervisord 出错:

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 these

root 2641 12938 0 04:52 pts/1 00:00:00 grep --color=auto supervisord

root29646 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

kill -s SIGTERM 29646

在/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.py4 directory = /srv/awesome/www5 user =yshi26 startsecs = 3

7

8 redirect_stderr = true

9 stdout_logfile_maxbytes =50MB10 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 日志文件大小,默认 50MB11 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 process3 ; 默认为 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 default2 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 supervisordsystemctl restart supervisord


版权声明:本文为weixin_39643338原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。