Supervisor安装与配置(linux/unix进程管理工具) Supervisor(http://supervisord.org)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。
因为Supervisor是Python开发的,安装前先检查一下系统否安装了Python2.4以上版本。下面以CentOS7.6,Python2.7.5版本环境下,介绍Supervisor的安装与配置步聚:
实验环境系统平台
cat /etc/redhat-releaseCentOS Linux release 7.6.1810 (Core)
Python版本
python -VPython 2.7.5
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y libffi-develwget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xztar xf Python-3.6.8.tar.xzcd Python-3.6.8./configure --prefix=/usr/local/python368make && make installecho \\\'export PATH=/usr/local/python368/bin:$PATH\\\' >> /etc/profilesource /etc/profilepython3 -V
安装 Supervisor 安装 Supervisor的方法很多,以下介绍三种,我这里所采用的为第三种
1、easy_install 安装 supervisor
安装Python包管理工具(easy_install) easy_install是setuptools包里带的一个命令,使用easy_install实际上是在调用setuptools来完成安装模块的工作,所以安装setuptools即可:
wget https://www.haocat.cn/aimg/bk47/uhaicmsubdffp4jrdcmmthc1oanh7eiwr6gtjrzr-0.webpunzip setuptools-33.1.1.zipcd setuptools-33.1.1python setup.py installeasy_install supervisor
2、pip 安装 supervisor
使用 pip 来安装,前提要保证pip版本大于2.6
pip install supervisor
3、yum epel-release 安装 supervisor
yum install -y epel-release && yum install -y supervisorsuperviso命令
supervisor命令 supervisor安装完成后会生成三个执行程序:supervisortd、supervisorctl、echo_supervisord_conf:
?supervisortd:用于管理supervisor本身服务?supervisorctl:用于管理我们需要委托给superviso工具的服务?echo_supervisord_conf:用于生成superviso的配置文件?supervisor的守护进程服务(用于接收进程管理命令)?客户端(用于和守护进程通信,发送管理进程的指令)
[root@Jumpserver /]# which supervisord/bin/supervisord[root@Jumpserver /]# which supervisorctl/bin/supervisorctl[root@Jumpserver /]# which echo_supervisord_conf/bin/echo_supervisord_conf
配置Supervisor 通过运行echo_supervisord_conf程序生成supervisor的初始化配置文件
如果使用yum安装则此步骤省略,直接进行修改配置文件步骤
mkdir /etc/supervisord.decho_supervisord_conf > /etc/supervisord.conf
修改配置文件
supervisor的配置文件内容有很多,不过好多都不需要修改就行使用,我这里只修改了以下两项
#修改socket文件的mode,默认是0700sed -i \\\'s/;chmod=0700/chmod=0766/g\\\' /etc/supervisord.conf ??#在配置文件最后添加以下两行内容来包含/etc/supervisord目录sed -i \\\'$a [include] files = /etc/supervisord.d/*.conf\\\' /etc/supervisord.conf
编写需要被Supervisor管理的进程 Supervisor只能管理非dameon进程,像默认的redis为前台运行、Tomcat其实是 startup.sh shutdown.sh来调用catalina.sh进行后台运行的,默认catalina.sh为前台运行的程序,不能管理像Nginx一样的非dameon进程
Tomcat被Supervisor管理
Tomcat安装如下:
wget http://us.mirrors.quenda.co/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47.tar.gzyum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 -ytar xf apache-tomcat-8.5.47.tar.gz ?-C /usr/local/mv /usr/local/apache-tomcat-8.5.47 /usr/local/tomcat
想要我们的应用被Supervisor管理,就需要在/etc/supervisord目录下编写配置文件,Tomcat案例如下:
vim /etc/supervisord.d/tomcat.conf[program:tomcat] ???????????????????????????????????????#程序唯一名称directory=/usr/local/tomcat ????????????????????????????#程序路径command=/usr/local/tomcat/bin/catalina.sh run ??????????#运行程序的命令autostart=true ?????????????????????????????????????????#是否在supervisord启动后tomcat也启动startsecs=10 ???????????????????????????????????????????#启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒autorestart=true ???????????????????????????????????????#程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启;意思为如果不是supervisord来关闭的该进程则认为不正当关闭,supervisord会再次把该进程给启动起来,只能使用该supervisorctl来进行关闭、启动、重启操作 startretries=3 ?????????????????????????????????????????#启动失败自动重试次数,默认是3user=root ??????????????????????????????????????????????#用哪个用户启动进程,默认是rootpriority=999 ???????????????????????????????????????????#进程启动优先级,默认999,假如Supervisord需要管理多个进程,那么值小的优先启动stopsignal=INTredirect_stderr=true ???????????????????????????????????#把stderr重定向到stdout标准输出,默认falsestdout_logfile_maxbytes=200MB ??????????????????????????#stdout标准输出日志文件大小,日志文件大小到200M后则进行切割,切割后的日志文件会标示为catalina.out1,catalina.out2,catalina.out3...,默认50MBstdout_logfile_backups = 100 ???????????????????????????#stdout标准输出日志文件备份数,保存100个200MB的日志文件,超过100个后老的将被删除,默认为10保存10个stdout_logfile=/usr/local/tomcat/logs/catalina.out ?????#标准日志输出位置,如果输出位置不存在则会启动失败stopasgroup=false ??????????????????????????????????????#默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程killasgroup=false ??????????????????????????????????????#默认为false,向进程组发送kill信号,包括子进程
启动进程 使用supervisord管理启动后,当你使用
/usr/local/tomcat/shutdown.sh或者kill $PID的时候,supervisord都会认为是意外关闭,会自动再次把进程拉起,除非是使用supervisord命令关闭
#supervisord启动supervisord -c /etc/supervisord.conf ???????????????????#启动supervisord进程,我们在配置文件中设置了 autostart=true 参数,在supervisord启动的时候 tomcat也随之启动ps -ef|grep java
程序管理
supervisorctl status tomcat ????????????????????????????#tomcat状态supervisorctl stop tomcat ??????????????????????????????#停止tomcatsupervisorctl start tomcat ?????????????????????????????#启动tomcatsupervisorctl restart tomcat ???????????????????????????#重启tomcatsupervisorctl reoload tomcat ???????????????????????????#重仔tomcat
Redis被Supervisor管理
redis默认不在配置文件中添加 daemonize yes 参数则是前台启动的,所以也可以被我们的的Supervisor所管理 redis配置文件如下:
cat redis6001.confport 6001bind 192.168.31.230protected-mode yespidfile "/usr/local/redis/run/redis6001.pid"loglevel noticelogfile "/usr/local/redis/logs/redis6001.log"save 900 1stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum ?yesdbfilename dump.rdbdir "/usr/local/redis/data/rdb/"timeout 0tcp-keepalive 300
编写redis被Supervisor管理的案例
vim /etc/supervisord.d/redis.conf[program:redis]directory=/usr/local/rediscommand=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis6001.confautostart=truestartsecs=10autorestart=truestartretries=3user=rootpriority=999stopsignal=INTredirect_stderr=truestdout_logfile_maxbytes=200MBstdout_logfile_backups = 100stdout_logfile=/usr/local/redis/logs/redis6001.logstopasgroup=falsekillasgroup=false
使用super启动redis
#关闭tomcatsupervisorctl stop tomcattomcat: stopped#杀掉supervisordps -ef|grep supervisordroot ????26927 ????1 ?0 10:47 ? ???????00:00:00 /usr/bin/python /bin/supervisord -c /etc/supervisord.confroot ????27549 27402 ?0 11:07 pts/2 ???00:00:00 grep --color=auto superkill -9 26927#重新启动supervisord使其重新加载配置文件,supervisord默认会把redis和tomcat都拉起来supervisord -c /etc/supervisord.conf
程序管理
supervisorctl status redis ?????????????????????????????#redis状态supervisorctl stop redis ???????????????????????????????#停止redissupervisorctl start redis ??????????????????????????????#启动redissupervisorctl restart reids ????????????????????????????#重启redissupervisorctl reoload redis ????????????????????????????#重载redis
程序管理 程序管理
supervisorctl status all ???????????????????????????#查看所有进程状态supervisorctl stop ??all ???????????????????????????#停止所有进程supervisorctl start ?all ???????????????????????????#启动所有进程supervisorctl restart all ??????????????????????????#重启所有进程supervisorctl reoload all ??????????????????????????#重载所有进程
Supervisord开启启动配置 vim /usr/lib/systemd/system/supervisord.service[Unit]Description=Process Monitoring and Control DaemonAfter=rc-local.service nss-user-lookup.target[Service]Type=forkingExecStart=/usr/bin/supervisord -c /etc/supervisord.conf[Install]WantedBy=multi-user.target
systemctl enable supervisordsystemctl is-enabled supervisord