supervisor是用Python开发的一套通用的进程管理程序,可以将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时可以自动重启。首先我们在运行环境安装并配置supervisor,这里我用的centos7。
安装supervisor
# yum search setuptools .... python-setuptools.noarch : Easily build and distribute Python packages #yum install -y python-setuptools.noarch #easy_install supervisor
创建主配置文件
# mkdir -m 755 -p /etc/supervisor \\创建supervisor配置文件目录 # echo_supervisord_conf >/etc/supervisor/supervisord.conf \\创建主配置文件 # cd /etc/supervisor/ # mkdir -m 755 conf.d \\创建项目配置文件目录
创建项目配置文件(运行3个脚本)
# vim conf.d/test.ini [program:tjapp_sendmessage] command=/bin/bash /data/shell/sendmessage.sh numprocs=1 autostart=true autorestart=true [program:bbscollection] command=/bin/bash /data/shell/bbscollection.sh numprocs=1 autostart=true autorestart=true [program:test_sbbscollection] command=/bin/bash /data/shell/test_sbbscollection.sh numprocs=1 autostart=true autorestart=true
这里如果不想把配置文件卸载一起,分开写也是可以的
在主配置文件中引入test.ini
#cat supervisord.conf ... [include] files = ./conf.d/*.ini
启动supervisor
# supervisord -c /etc/supervisord.conf
查看supervisor运行的脚本
# supervisorctl bbscollection RUNNING pid 10090, uptime 4 days, 17:20:10 test_sbbscollection RUNNING pid 10088, uptime 4 days, 17:20:10 tjapp_sendmessage RUNNING pid 10089, uptime 4 days, 17:20:10
停止bbscollection脚本
# supervisorctl stop bbscollection bbscollection: stopped [root@om-web_test supervisord.d]# supervisorctl bbscollection STOPPED Apr 06 10:23 AM test_sbbscollection RUNNING pid 10088, uptime 4 days, 17:23:13 tjapp_sendmessage RUNNING pid 10089, uptime 4 days, 17:23:13 supervisor> start bbscollection \\启动 supervisor> status bbscollection RUNNING pid 7310, uptime 0:00:24 test_sbbscollection RUNNING pid 10088, uptime 4 days, 17:23:54 tjapp_sendmessage RUNNING pid 10089, uptime 4 days, 17:23:54
下面跑一个案例
program:itchatserver] command=/usr/local/python3/bin/python3 scwechat.py process_name=%(program_name)s numprocs=1 directory=/srv/www/application priority=998 autostart=true autorestart=true startsecs=1 startretries=3 user=root redirect_stderr=true stdout_logfile_maxbytes=1MB stdout_logfile_backups=10 stdout_capture_maxbytes=1MB stdout_events_enabled=false stderr_logfile_maxbytes=1MB stderr_logfile_backups=10 stderr_capture_maxbytes=1MB stderr_events_enabled=false
配置文件通过[program:itchatserver]
指定服务名为itchatserver,command
指定启动scwechat.py
。
然后重启Supervisor后,就可以随时启动和停止Supervisor管理的服务了:
$ sudo supervisorctl reload $ sudo supervisorctl start itchatserver $ sudo supervisorctl status itchatserver RUNNING pid 1401, uptime 5:01:34
当然在安装后使用有可能报错的:unix:///tmp/supervisor.sock no such file
解决unix:///tmp/supervisor.sock no such file的问题 1、修改supervisord的配置文件 vim /etc/supervisord.conf 这里把所有的/tmp路径改掉, /tmp/supervisor.sock 改成 /var/run/supervisor.sock, /tmp/supervisord.log 改成 /var/log/supervisor.log, /tmp/supervisord.pid 改成 /var/run/supervisor.pid 要不容易被linux自动清掉 此时即使正常启动了supervisord,并且管理的进程也在后台运行, 但是当在命令行执行supervisorctl status还是会报unix:///tmp/supervisor.sock no such file 所以还需要修改如下配置:serverurl=unix:///tmp/supervisor.sock 改成serverurl=unix:///var/run/supervisor.sock, 否则supervisord还是默认去tmp下找sockt文件。此时再执行supervisorctl status就会正常显示进程的状态信息了。 2、修改权限 sudo chmod 777 /run sudo chmod 777 /var/log 如果没改,启动报错 IOError: [Errno 13] Permission denied: '/var/log/supervisord.log' 3、创建supervisor.sock sudo touch /var/run/supervisor.sock sudo chmod 777 /var/run/supervisor.sock 4、启动supervisord,注意stop之前的实例或杀死进程 supervisord