2020年年后工做中需开发一支持多数据源自动上报业务数据的程序,程序开发完部署上线时须要对其进程进行自动管理,否则哪天程序down了还不知,可就麻烦了,因此这里选用了强大的supervisor,如下文章为学习及实操时的笔记,不正确处请指出
python
Supervisor官网:http://supervisord.org
Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。它是经过fork/exec的方式把这些被管理的进程看成supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去便可。也实现当子进程挂掉的时候,父进程能够准确获取子进程挂掉的信息的,能够选择是否本身启动和报警。
supervisor还提供了一个功能,能够为supervisord或者每一个子进程,设置一个非root的user,这个user就能够管理它对应的进程。git
[maaiqiang@localhost ~]$ pip install supervisor
github
[maaiqiang@localhost ~]$ ls supervisor-4.2.0.tar.gz [maaiqiang@localhost ~]$ tar xf supervisor-4.2.0.tar.gz [maaiqiang@localhost ~]$ cd supervisor-4.2.0 [maaiqiang@localhost ~]$ python setup.py install
[unix_http_server] file=/tmp/supervisor.sock ;UNIX socket 文件,supervisorctl 会使用 ;chmod=0700 ;socket文件的mode,默认是0700 ;chown=nobody:nogroup ;socket文件的owner,格式:uid:gid ;[inet_http_server] ;HTTP服务器,提供web管理界面 ;port=127.0.0.1:9001 ;Web管理后台运行的IP和端口,若是开放到公网,须要注意安全性 ;username=user ;登陆管理后台的用户名 ;password=123 ;登陆管理后台的密码 [supervisord] logfile=/tmp/supervisord.log ;日志文件,默认是 $CWD/supervisord.log logfile_maxbytes=50MB ;日志文件大小,超出会rotate,默认 50MB,若是设成0,表示不限制大小 logfile_backups=10 ;日志文件保留备份数量默认10,设为0表示不备份 loglevel=info ;日志级别,默认info,其它: debug,warn,trace pidfile=/tmp/supervisord.pid ;pid 文件 nodaemon=false ;是否在前台启动,默认是false,即以 daemon 的方式启动 minfds=1024 ;能够打开的文件描述符的最小值,默认 1024 minprocs=200 ;能够打开的进程数的最小值,默认 200 [supervisorctl] serverurl=unix:///tmp/supervisor.sock ;经过UNIX socket链接supervisord,路径与unix_http_server部分的file一致 ;serverurl=http://127.0.0.1:9001 ; 经过HTTP的方式链接supervisord ; [program:xx]是被管理的进程配置参数,xx是进程的名称 [program:xx] command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run ; 程序启动命令 autostart=true ; 在supervisord启动的时候也自动启动 startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒 autorestart=true ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启 startretries=3 ; 启动失败自动重试次数,默认是3 user=tomcat ; 用哪一个用户启动进程,默认是root priority=999 ; 进程启动优先级,默认999,值小的优先启动 redirect_stderr=true ; 把stderr重定向到stdout,默认false stdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默认50MB stdout_logfile_backups = 20 ; stdout 日志文件备份数,默认是10 ; stdout 日志文件,须要注意当指定目录不存在时没法正常启动,因此须要手动建立目录(supervisord 会自动建立日志文件) stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out stopasgroup=false ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程 killasgroup=false ;默认为false,向进程组发送kill信号,包括子进程 ;包含其它配置文件 [include] files = relative/directory/*.ini ;能够指定一个或多个以.ini结束的配置文件
[maaiqiang@localhost ~]$ mkdir /etc/supervisor [maaiqiang@localhost ~]$ echo_supervisord_conf > /etc/supervisor/supervisored.conf
;conf.d 为配置表目录,须要手动建立,方便各子进程配置管理 [include] files = conf.d/*.conf
[maaiqiang@localhost ~]$ mkdir /etc/supervisor/conf.d
[maaiqiang@localhost ~]$ vi UmsScheduler.conf
web
[program:UmsScheduler] command=/usr/bin/python /home/mq/ums/py/UmsMonitor/UmsScheduler.py directory=/home/mq/ums/py/UmsMonitor/ autostart=true autorestart=true stderr_logfile=/home/mq/ums/py/UmsMonitor/log/UmsScheduler-err.log stdout_logfile=/home/mq/ums/py/UmsMonitor/log/UmsScheduler-out.log user=mq
[maaiqiang@localhost ~]$ supervisord -c /etc/supervisor/supervisord.conf
apache
# dservice for systemd (CentOS 7.0+) # by ET-CS (https://github.com/ET-CS) [Unit] Description=Supervisor daemon [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf ExecStop=/usr/bin/supervisorctl shutdown ExecReload=/usr/bin/supervisorctl reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
[maaiqiang@localhost ~]$ systemctl enable supervisord.service # 加入开启自启动项 [maaiqiang@localhost ~]$ systemctl is-enabled supervisord #检查是否为开启启动项 [maaiqiang@localhost ~]$ systemctl start supervisord.service # 启动并加载默认配置文件
[maaiqiang@localhost ~]$ ps -ef | grep 'supervisord'
[maaiqiang@localhost ~]$ ps -ef | grep 'UmsScheduler'
这里,能够模拟发生异常将其进程杀死,查看其进程是否可以自动启动tomcat
supervisorctl status # 查看全部进程的状态 supervisorctl stop UmsScheduler # 中止UmsScheduler supervisorctl start UmsScheduler # 启动UmsScheduler supervisorctl restart UmsScheduler # 重启UmsScheduler supervisorctl update # 配置文件修改后使用命令加载新的配置 supervisorctl reload # 从新启动配置中的全部程序