- 安装supervisor
使用easy_install(其它方式也有,自行解决)
easy_install supervisor
2.配置
初始化配置
echo_supervisord_conf > /etc/supervisord.conf
会在/etc/目录下生产一个supervisord.conf的配置文件,修改一下地方,
[unix_http_server]
file=/var/run/supervisor.sock ; UNIX socket 文件,supervisorctl 会使用(原为tmp目录下,听说在tmp目录下文件会被删除,所以修改目录,没测试过)
...
[supervisord]
;logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile=/var/log/supervisord.log ; main log file; default $CWD/supervisord.log
...
[supervisorctl]
;serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
...
[include]
;files = relative/directory/*.ini
files = /etc/supervisor/*.conf ; 这里包含文件可以让你在不同的文件做不同的配置,避免在这个文件中一直追加配置(子配置)
2.1 添加子配置
根据include的配置
[include]
files = /etc/supervisor/*.conf
在/etc/supervisor/目录下添加配置文件example.conf
[program:example]
command=python xxx.py
directory=/xxx/
process_name=%(program_name)s_%(process_num)s
numprocs=1
user=root
autostart=true
autorestart=true
stdout_logfile=/xxx/supervisor_queue.log
stderr_logfile=/xxx/exam_imagick/supervisor_queue.log
[program:example] 进程名
directory=/root/exam_imagick/ 项目路径
command=python start.py 执行的命令
xxx为你的项目目录,这样配置会在你项目目录下生成输出日志和错误日志
- 开启服务
supervisord -c /etc/supervisord.conf
- 使用supervisorctl管理进程
supervisorctl status
supervisorctl stop xxx
supervisorctl start xxx
supervisorctl restart xxx
supervisorctl reread
supervisorctl update
- 开启服务可能失败的问题
我只遇到这种问题其它问题请google
Starting supervisor: 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.
根据的配置文件,删除对应目录下的supervisor.sock文件,然后重新supervisord -c /etc/supervisor.conf
unlink /var/run/supervisor.sock