在高可用服务中,我们需要保活进程,行业中使用最广泛的开源组件就是Supervisor,本文简单介绍一下如何快速使用supervisor进行进程保活。
root账号执行一下命令
yum -y install epel-release
yum install python-pip
pip install --upgrade pip
pip install supervisor
# 如果需要通过代理,则执行命令
pip install --upgrade pip --proxy {proxy_host:proxy_port}
pip install supervisor --proxy {proxy_host:proxy_port}
setuptools https://pypi.org/project/setuptools/#files
supervisor https://pypi.org/project/supervisor/#files
meld3 https://pypi.org/project/meld3/#files
unzip setuptools-40.6.3.zip
cd setuptools-40.6.3
sudo python setup.py install
tar xzf meld3-1.0.2.tar.gz
cd meld3-1.0.2
sudo python setup.py install
tar xzf supervisor-3.3.4.tar.gz
cd supervisor-3.3.4
sudo python setup.py install
echo_supervisord_conf > /etc/supervisord.conf
启动supervisord
supervisord -c /etc/supervisord.conf
上面我们已经把 supervisrod 运行起来了,现在可以添加我们要管理的进程的配置文件。可以把所有配置项都写到 supervisord.conf 文件里,但并不推荐这样做,而是通过 include 的方式把不同的程序(组)写到不同的配置文件里,对,就是默认配置中的最后的那个include。修改后如下:
[include]
files = /etc/supervisor/*.conf
我们的配置文件在/etc/supervisor/bot.conf
[program:taskbot]
command=/home/tenmao/bot-tomcat/bin/catalina.sh run
directory=/home/tenmao/bot-tomcat ; directory to cwd to before exec (def no cwd)
autostart=true ; start at supervisord start (default: true)
startsecs=1 ; # of secs prog must stay up to be running (def. 1)
startretries=3 ; max # of serial start failures when starting (default 3)
autorestart=true ; when to restart if exited after running (def: unexpected)
stopasgroup=false ; send stop signal to the UNIX process group (default false)
killasgroup=true ; SIGKILL the UNIX process group (def false)
stdout_logfile=/home/tenmao/supervisord/log/%(program_name)s-out.log ; stdout log path, NONE for none; default AUTO
supervisorctrl start/stop/restart [program_name]
supervisorctrl status
supervisorctrl update [all]
重新加载配置文件,并根据新文件增加或删除对应的进程
catalina.sh start
,而需要使用catalina.sh run
supervisorctl status
: error: <class 'socket.error'>, [Errno 113] No route to host: file: /usr/lib64/python2.7/socket.py line: 571
解决办法:supervisorctl -c supervisor/supervisord.conf status