CentOS 6.3安装配置supervisor进程管理工具
http://www.linuxidc.com/Linux/2012-11/73832.htm
1. Supervisor是一个C/S系统,它可以在类unix操作系统让用户来监视和控制后台服务进程的数量,一个很重要的功能就是监控服务器的主要后台进程,并在出现问题是自动重启。
2. 根据服务器上的python版本下载对应的setuptools
[root@test1 ~]# python -V
Python 2.6.6
wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg#md5=bfa92100bd772d5a213eedd356d64086
直接安装
sh setuptools-0.6c11-py2.6.egg
3. 下载并安装supervisor
wget http://pypi.python.org/packages/source/s/supervisor/supervisor-3.0b1.tar.gz
tar -zxvf supervisor-3.0b1.tar.gz
cd supervisor-3.0b1
python setup.py install
安装setuptools后也可以
easy_install supervisor
;centos中,安装easy_install后
需要建立软链接,系统才能找到eaxy_install-2.7的命令,ln -s /usr/local/bin/easy_install-2.7 /usr/bin/easy_install-2.7
;centos中,easy_install supervisor 千万不要在虚拟环境中安装
4. 设定supervisor配置文件
创建默认的配置文件
echo_supervisord_conf >/etc/supervisord.conf
vi /etc/supervisord.conf
加入你的配置
##################################################################我的一个实例配置
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Note: shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
[unix_http_server]
file=/home/sy/supervisor/supervisor1.sock ; (the path to the socket file) ;注意这里的/home/sy/supervisor/目录必须手动创建,否则服务器无法启动起来,会提示没有这个目录
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
[inet_http_server] ; inet (TCP) server disabled by default
port=152.14.1.54:9001 ; (ip_address:port specifier, *:port for all iface) ,这里不要用127.0.0.1或者localhost,否则外网没法访问
username=yx ; (default is no username (open server))
password=123456 ; (default is no password (open server))
[supervisord]
logfile=/home/sy/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) ;注意这里的/home/sy/supervisor/必须手动创建,否则服务器无法启动起来,会提示没有这个目录
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/home/sy/supervisor/supervisord.pid ; (supervisord pidfile;default supervisord.pid) ;注意这里的路径必须和下面的/etc/init.d/supervisord的路径一致,否则对应不起来, /home/sy/supervisor/目录必须手动创建
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
;umask=022 ; (process file creation umask;default 022)
;user=chrism ; (default is current user, required if root)
;identifier=supervisor ; (supervisord identifier, default is 'supervisor')
;directory=/tmp ; (default is not to cd during start)
;nocleanup=true ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value ; (key value pairs to add to environment)
;strip_ansi=false ; (strip ansi escape codes in logs; def. false)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///home/sy/supervisor/supervisorunix.sock ; use a unix:// URL for a unix socket ;注意这里的/home/sy/supervisor/必须手动创建,否则服务器无法启动起来,会提示没有这个目录
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as http_username if set
;password=123 ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
###################################################################
5. 设定supervisor启动文件---拷贝的时候,把里面的中文注释“特别注意....”那些去掉
vi /etc/init.d/supervisord
#!/bin/sh # # /etc/rc.d/init.d/supervisord # # supervisor is a client/server system that # allows its users to monitor and control a # number of processes on UNIX-like operating # systems. # # chkconfig: - 64 36 # description: Supervisor Server # processname: supervisord # Source init functions . /etc/init.d/functions RETVAL=0 #特别注意,pidfile路径和自定义的supervisord.conf[第8条里面的pidfile对应]里面的pidfile路径对应,默认放在tmp下面,但是生产环境不要放在tmp目录下,因为tmp会定时删除定时没有改变的文件,导致supervisord不能启动等bug pidfile="/home/sy/supervisor/supervisord.pid" #注意这里必须和上面的配置superconfig.conf的pidfile路径一样 lockfile="/home/sy/supervisor/supervisord" #这里的路径最好和上面的路径同一起来 用/home/sy/supervisor/目录代替/var/lock/subsys/supervisord start() { echo -n $"Starting $prog: " daemon --pidfile $pidfile supervisord -c /etc/supervisord.conf RETVAL=$? echo [ $RETVAL -eq 0 ] && touch ${lockfile} } stop() { echo -n $"Shutting down $prog: " killproc -p ${pidfile} /usr/bin/supervisord RETVAL=$? echo if [ $RETVAL -eq 0 ] ; then rm -f ${lockfile} ${pidfile} fi } case "$1" in start) start ;; stop) stop ;; status) status $prog ;; restart) stop start ;; *) echo "Usage: $0 {start|stop|restart|status}" ;; esac
注意:如果是centos7 ,在/etc/init.d/下面加了文件,或者修改了文件,需要执行命令: systemctl daemon-reload
centos7 ,status supervisord.service 这个命令可以查看为什么服务启动失败
特别注意: 当在anconda上部署django和uwsgi的时候遇到这个错误ImportError: No module named site, 千万别用service supervisord start的方式启动,这个太坑了
/usr/bin/supervisord -c /etc/supervisord.conf 用这个启动
service supervisord stop 可以用这个停止
6.权限问题
chmod 777 /etc/init.d/supervisord
chmod 777 /etc/supervisord.conf
;centos中, ln -s /usr/local/bin/supervisord /usr/bin/supervisord,才能找到supervisord命令
;centos中,添加服务chkconfig --add supervisord chkconfig supervisord on
;centos中,启动service supervisord start
;centos中,service supervisord stop也可以停止supervisord ps -ef |grep supervisord 根据进程号kill
7.supervisord开启久了后,会僵死【有可能是把第五条或者第8条的pidfile放在tmp目录下导致的】,这时候service supervisord stop无效,必须supervisord ps -ef |grep supervisord 根据进程号kill,注意这里不要kill -9 否则结束不了supervisor里面运行的程序,直接kill 进程号
如果里面的程序还在运行,这个时候可以重启服务器,也可以 ps -ef|grep python2.7 找到正则运行的服务,kill掉
再将redis kill掉
最后在service supervisrod start
8.supervisord的日志一定不要保存在tmp目录下,因为tmp目录系统会定时删除没有更改过的日志,下面的配置可以修改supervisor的日志目录
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) #这些都不要放在tmp里面,不然日志会丢失
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) #这里和第五条的特别注意呼应
childlogdir=/var/log/supervisor