准备
需要python2.7版本以上才能安装,如果是CentOS6,一般自带得是2.6.6版本,需要先升级python,详情查看:http://www.884358.com/centos6-python27/
查看python版本:
python -V
下载
下载地址:https://pypi.org/project/supervisor/
我下载的是4.1.0版本。
安装
tar -zxvf supervisor-4.1.0.tar.gz
cd supervisor-4.1.0
python setup.py install
如若运行报错:
Traceback (most recent call last):
File “setup.py”, line 32, in
from setuptools import setup, find_packages
ImportError: No module named setuptools
则需要安装setuptools模块
setuptools-0.6c11-py2.7.egg
然后运行:
sh setuptools-0.6c11-py2.7.egg
之后再重新执行python setup.py install
建立软连接
mv /usr/bin/echo_supervisord_conf /usr/bin/echo_supervisord_conf_bak
ln -s /usr/local/bin/echo_supervisord_conf /usr/bin/echo_supervisord_conf
mv /usr/bin/supervisord /usr/bin/supervisord_bak
ln -s /usr/local/bin/supervisord /usr/bin/supervisord
测试supervisord是否安装成功
echo_supervisord_conf
建立应用配置文件夹
mkdir -p /etc/supervisor/conf.d/
创建默认的配置文件,并修改配置
echo_supervisord_conf >/etc/supervisor/supervisord.conf
vi /etc/supervisor/supervisord.conf
#在末尾添加
[include]
files = ./conf.d/*.conf
设置程序
以短信发送程序为例:
创建/etc/supervisor/conf.d/sendsms.conf
文件,内容如下:
[program:sendsms]
environment = STNORESTART="1", HOME="/root"
directory = /root/
command=php /www/cmder/send.php
autostart=true
autorestart=true
user=root
redirect_stderr=true
stdout_logfile=/var/log/sendsms.log
设置启动脚本
在/etc/init.d/
目录新建supervisord文件
vim /etc/init.d/supervisord
内容如下:
#!/bin/bash
#
# supervisord This scripts turns supervisord on
# chkconfig: 345 83 04
# description: supervisor is a process control utility. It has a web based
# xmlrpc interface as well as a few other nifty features.
#
# source function library
. /etc/rc.d/init.d/functions
set -a
PREFIX=/usr/local
SUPERVISORD=$PREFIX/bin/supervisord
SUPERVISORCTL=$PREFIX/bin/supervisorctl
PIDFILE=/var/supervisor/supervisord.pid
LOCKFILE=/var/supervisor/supervisord.lock
OPTIONS="-c /etc/supervisor/supervisord.conf"
# unset this variable if you don't care to wait for child processes to shutdown before removing the $LOCKFILE-lock
WAIT_FOR_SUBPROCESSES=yes
# remove this if you manage number of open files in some other fashion
ulimit -n 96000
RETVAL=0
running_pid()
{
# Check if a given process pid's cmdline matches a given name
pid=$1
name=$2
[ -z "$pid" ] && return 1
[ ! -d /proc/$pid ] && return 1
(cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1
return 0
}
running()
{
# Check if the process is running looking at /proc
# (works for all users)
# No pidfile, probably no daemon present
[ ! -f "$PIDFILE" ] && return 1
# Obtain the pid and check it against the binary name
pid=`cat $PIDFILE`
running_pid $pid $SUPERVISORD || return 1
return 0
}
start() {
echo "Starting supervisord: "
if [ -e $PIDFILE ]; then
echo "ALREADY STARTED"
return 1
fi
# start supervisord with options from sysconfig (stuff like -c)
$SUPERVISORD $OPTIONS
# show initial startup status
$SUPERVISORCTL $OPTIONS status
# only create the subsyslock if we created the PIDFILE
[ -e $PIDFILE ] && touch $LOCKFILE
}
stop() {
echo -n "Stopping supervisord: "
$SUPERVISORCTL $OPTIONS shutdown
if [ -n "$WAIT_FOR_SUBPROCESSES" ]; then
echo "Waiting roughly 60 seconds for $PIDFILE to be removed after child processes exit"
for sleep in 2 2 2 2 4 4 4 4 8 8 8 8 last; do
if [ ! -e $PIDFILE ] ; then
echo "Supervisord exited as expected in under $total_sleep seconds"
break
else
if [[ $sleep -eq "last" ]] ; then
echo "Supervisord still working on shutting down. We've waited roughly 60 seconds, we'll let it do its thing from here"
return 1
else
sleep $sleep
total_sleep=$(( $total_sleep + $sleep ))
fi
fi
done
fi
# always remove the subsys. We might have waited a while, but just remove it at this point.
rm -f $LOCKFILE
}
restart() {
stop
start
}
case "$1" in
start)
start
RETVAL=$?
;;
stop)
stop
RETVAL=$?
;;
restart|force-reload)
restart
RETVAL=$?
;;
reload)
$SUPERVISORCTL $OPTIONS reload
RETVAL=$?
;;
condrestart)
[ -f $LOCKFILE ] && restart
RETVAL=$?
;;
status)
$SUPERVISORCTL $OPTIONS status
if running ; then
RETVAL=0
else
RETVAL=1
fi
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
exit 1
esac
exit $RETVAL
其中,需要注意以下变量的设置:
PREFIX=/usr/local
SUPERVISORD=$PREFIX/bin/supervisord #supervisord 程序的安装路径
SUPERVISORCTL=$PREFIX/bin/supervisorctl #supervisorctl 程序的安装路径
PIDFILE=/var/supervisor/supervisord.pid #需要先创建/var/supervisor目录
LOCKFILE=/var/supervisor/supervisord.lock
OPTIONS="-c /etc/supervisor/supervisord.conf" #配置文件的路径
保存完毕之后,可以执行以下命令修改文件权限:
chmod 755 /etc/init.d/supervisord
配置开机启动
chkconfig supervisord on
可以以下命令查看是否成功
chkconfig --list | grep supervisord
启动
方式一:
/etc/init.d/supervisord start
方式二:
#启动命令
supervisord
-c, --configuration 指定配置文件路径 (默认为/etc/supervisord.conf)
-i, --interactive 执行命令后启动交互式shell
-s, --serverurl URL upervisord服务器监听的URL(默认为“ http:// localhost:9001 ”)
-u, --username 用于与服务器进行身份验证的用户名
-p, --password 用于与服务器进行身份验证的密码
-r, --history-file 保留readline历史记录(如果readline可用)
supervisord -c /etc/supervisor/supervisord.conf
重启
supervisorctl reload
常用命令
supervisorctl stop sendsms,停止某一个进程(sendsms),sendsms[program:sendsms]里配置的值,sendsms。
supervisorctl start sendsms,启动某个进程
supervisorctl restart sendsms,重启某个进程
supervisorctl stop groupworker: ,重启所有属于名为groupworker这个分组的进程(start,restart同理)
supervisorctl stop all,停止全部进程,注:start、restart、stop都不会载入最新的配置文件。
supervisorctl reload,载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。
supervisorctl update,根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。
注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。
查看日志
默认是在/var/log/supervisord.log
也可能是在/tmp/supervisord.log
开启web管理端
修改/etc/supervisor/supervisord.conf文件,内容如下:
[inet_http_server]
port=0.0.0.0:9001
username=xxx
password=xxx
其他
unix:///tmp/supervisor.sock no such file
使用supervisorctl start
命令时报以上错误。解决办法如下:
- 打开配置文件
/etc/supervisor/supervisord.conf
- 把所有的
/tmp
路径都替换掉:/tmp/supervisor.sock
改成/var/run/supervisor.sock
/tmp/supervisord.log
改成/var/log/supervisor.log
/tmp/supervisord.pid
改成/var/run/supervisor.pid
-
修改权限
chmod 777 /run
chmod 777 /var/log
- 创建 supervisor.sock
touch /var/run/supervisor.sock
chmod 777 /var/run/supervisor.sock
- 杀掉supervisor进程。
查看进程号
ps -elf | grep "supervisor"
杀死进程
kill -9 19737
- 启动supervisord
supervisord -c /etc/supervisor/supervisord.conf
参考:
https://www.cnblogs.com/sfnz/p/5578417.html
https://blog.csdn.net/shooke/article/details/85604273
https://blog.csdn.net/youzi_yun/article/details/84934110
https://www.cnblogs.com/apexchu/p/4207333.html