supervisor 是用python开发的一套通用进程管理程序,能将一个普通的命令进程变成后台daemon,并监控进程状态,异常退出时能自动重启。

相对的Linux后台进程管理还有 nohup, screen

https://pypi.python.org/simple/setuptools/
https://pypi.python.org/simple/meld3/
https://pypi.python.org/simple/supervisor/

安装 supervisor 事先需要安装好 setuptools 和 meld3,安装完毕后即可安装 supervisor

1、安装 setuptools python-setuptools-0.6.10-3.el6

yum install python-setuptools

2、安装 meld3 meld3-1.0.1.tar.gz

wget https://pypi.python.org/packages/0f/5e/3a57c223d8faba2c3c2d69699f7e6cfdd1e5cc31e79cdd0dd48d44580b50/meld3-1.0.1.tar.gz#md5=2f045abe0ae108e3c8343172cffbe21d
tar zxvf meld3-1.0.1.tar.gz

python setup.py install

3、安装 supervisor-3.1.4.tar.gz

wget https://pypi.python.org/packages/12/50/cd330d1a0daffbbe54803cb0c4c1ada892b5d66db08befac385122858eee/supervisor-3.1.4.tar.gz#md5=cb64f92409ebabf17aa0884fc407fec6
tar zxvf supervisor-3.1.4.tar.gz
cd supervisor-3.1.4/
python setup.py install

4、生成默认配置文件

echo_supervisord_conf > /etc/supervisord.conf

创建每个进程配置文件目录

mkdir -p /etc/supervisor

/etc/supervisor/ 配置每个进程的内容

修改 /etc/supervisord.conf 的 include,指引配置文件

[include]
files = /etc/supervisor/*.conf

注意,记得将 [include]前面的分号去掉,不然 include将无法生效,这块折腾了自己1个小时

添加一条测试内容

[program:test-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /work/test/artisan queue:work --queue=high,default,low
autostart=true
autorestart=true
numprocs=2
redirect_stderr=true
stdout_logfile=/work/test/storage/logs/worker.log

启动

supervisord -c /etc/supervisord.conf

supervisord启动成功后,可以通过supervisorctl客户端控制进程,启动、停止、重启

supervisorctl status
supervisorctl stop test-worker:*
supervisorctl start test-worker:*
supervisorctl restart test-worker:*
supervisorctl reread
supervisorctl update