参考链接:https://blog.csdn.net/xyang81/article/details/51555473
Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。
因为Supervisor是Python开发的,安装前先检查一下系统否安装了Python2.4以上版本。下面以CentOS7.4,Python2.7版本环境下,介绍Supervisor的安装与配置步聚
1,环境检查
2,安装supervisor
yum install python-setuptools easy_install pip pip install superviso
3,配置
创建配置文件夹
mkdir /etc/supervisor
运行echo_supervisord_conf程序生成supervisor的初始化配置文件
echo_supervisord_conf > /etc/supervisor/supervisord.conf
修改该配置文件
[unix_http_server] file=/tmp/supervisor.sock ; the path to the socket file [inet_http_server] ; 开启web管理界面 port=*:9001 ; 默认端口9001 *不限制登录的客户端 username=user ; 用户名 password=123 ; 密码 [supervisord] logfile=/var/log/supervisord.log ;日志 logfile_maxbytes=50MB ; 最大日志大小50M logfile_backups=10 ; # of main logfile backups; 0 means none, default 10 loglevel=info ; 日志等级 pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid 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 [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket [include] files = /etc/supervisor/config.d/*.ini #进程管理配置文件单独放置在一个目录以ini结尾
4,启动
supervisord -c /etc/supervisor/supervisord.conf
web页面登录(配置用户名user密码123)
5,配置管理进程
/etc/supervisor/config.d/tomcat.ini
program:tomcat] environment = JAVA_HOME="/opt/jdk1.7.0_45" command=/opt/apache-tomcat-7.0.56/bin/catalina.sh run stdout_logfile=/opt/apache-tomcat-7.0.56/logs/catalina.out autostart=true autorestart=true startsecs=5 priority=1 stopasgroup=true killasgroup=true
PS:environment设置java环境家目录,如果是yum安装的jdk不需要设置,如果不设置不能通过supervisor启动tomcat会报spawn error错误
重启
supervisorctl reload
supervisord启动成功后,可以通过supervisorctl客户端控制进程,启动、停止、重启。运行supervisorctl命令,不加参数,会进入supervisor客户端的交互终端,并会列出当前所管理的所有进程。
输入help可以查看可以执行的命令列表,如果想看某个命令的作用,运行help 命令名称,如:help stop
通过web界面管理
6,开机启动supervisor
进入/lib/systemd/system目录,并创建supervisor.service文件
[Unit] Description=supervisor After=network.target [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown ExecReload=/usr/bin/supervisorctl $OPTIONS reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
设置开机启动
systemctl enable supervisor.service systemctl daemon-reload
修改权限为766
chmod 766 supervisor.service
设置service服务类型
编辑服务 /etc/rc.d/init.d/supervisor
#!/bin/bash # # supervisord This scripts turns supervisord on # # Author: Mike McGrath <mmcgrath@redhat.com> (based off yumupdatesd) # # chkconfig: - 95 04 # # description: supervisor is a process control utility. It has a web based # xmlrpc interface as well as a few other nifty features. # processname: supervisord # config: /etc/supervisor/supervisord.conf # pidfile: /var/run/supervisord.pid # # source function library . /etc/rc.d/init.d/functions RETVAL=0 start() { echo -n $"Starting supervisord: " daemon "supervisord -c /etc/supervisor/supervisord.conf " RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord } stop() { echo -n $"Stopping supervisord: " killproc supervisord echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/supervisord } restart() { stop start } case "$1" in start) start ;; stop) stop ;; restart|force-reload|reload) restart ;; condrestart) [ -f /var/lock/subsys/supervisord ] && restart ;; status) status supervisord RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}" exit 1 esac exit $RETVAL
设置自启动
chmod 755 /etc/rc.d/init.d/supervisor chkconfig supervisor on
ps:注意:修改脚本中supervisor配置文件路径为你的supervisor的配置文件路径
PS:Supervisor只能管理非daemon的进程,也就是说Supervisor不能管理守护进程。否则提示Exited too quickly (process log may have details)异常。例子中的Tomcat默认是以守护进程启动的,所以我们改成了catalina.sh run,以前台进程的方式运行。
PS:日志文件/var/log/supervisord.log记录程序启动过程