linux的后台进程运行有好几种方法,例如nohup,screen等,但是,如果是一个服务程序,要可靠地在后台运行,我们就需要把它做成daemon,最好还能监控进程状态,在意外结束时能自动重启。

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

centos安装supervisor:

yum install python-setuptools
easy_install supervisor

echo_supervisord_conf > /etc/supervisord.conf
//生成配置文件

vim /etc/supervisord.conf
//在supervisord.conf最下面新增配置
------
[program:bandwidth]
command=python26 /usr/local/bin/bandwidth.sh
//需要执行的命令
user =root
//(default  is  current  user , required  if  root)
autostart=true
//start at supervisord start (default: true)
autorestart=true
//whether/when to restart (default: unexpected)
startsecs=3
//number of secs prog must stay running ( def . 1)
stderr_logfile=/tmp/bandwidth_err.log
//redirect proc stderr to stdout (default false) 错误输出重定向
stdout_logfile=/tmp/bandwidth.log
//stdout log path, NONE  for  none; default AUTO, log输出

//更多配置说明请参考:http://supervisord.org/configuration.html
------

supervisord -c /etc/supervisord.conf
//启动supervisor

Debian/Ubuntu 安装supervisor:

apt-get install supervisor
vim /etc/supervisor/conf.d/bandwidth.conf
//新增一个配置文件
------
[program:bandwidth]
command = python26 /usr/local/bin/bandwidth.sh
directory = /usr/local/bin/
user = root
autostart = true
autorestart = true
//配置同上,不过需要注意一定要有directory
------

supervisorctl update
supervisorctl start bandwidth

常用状态命令:

supervisorctl
//直接进入命令行模式
supervisorctl status
//查看运行状态
supervisorctl help
//查看帮助
supervisorctl reload
//重载配置文件
supervisorctl stop [name]
//停止运行[name]
supervisorctl start [name]
//开始运行[name]

//更多命令请参考:http://supervisord.org/