Linux后台进程管理(守护进程)supervisor

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

安装supervisor

Debian / Ubuntu可以直接通过apt安装:

# apt-get install supervisor

配置文件可以单独分拆,放在/etc/supervisor/conf.d/目录下,以.conf作为扩展名

[program:SendEmailList]
command=/usr/bin/php /www/shell/basics/SendEmailList.php >> /dev/null
directory=/www/shell/basics
user=root

定义[program:SendEmailList] 进程Send名称 ,command是命令,directory是进程的当前目录,user是进程运行的用户身份。

supervisorctl start SendEmailList
supervisorctl stop SendEmailList

如果要在命令行中使用变量,就需要自己先编写一个shell脚本:

#!/bin/sh /usr/bin/gunicorn -w `grep -c ^processor /proc/cpuinfo` wsgiapp:application

然后,加上x权限,再把command指向该shell脚本即可。
supervisor还有许多选项,默认的autorestart为unexpected(异常退出),具体请参考supervisor文档。