Linux - supervisor

supervisor是什么?

Supervisor是在linux上的进程管理员,是一个管理工具。当进程停止的时候Supervisor能够自动启动它,可以运行在各种类unix的机器上,不支持windows系统运行。Supervisor运行在python3.4版本及以上版本和python2.7。

Supervisor是基于Python语言开发的一套的进程管理程序,能够把普通脚本、命令行进程变成后台的daemon,并监控状态,实现自动重启,并提供可视化管理界面。

工作原理

supervisor通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动。当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和预警。

supervisor 可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。

组成

supervisord:supervisor服务的主要管理器,负责管理我们配置的子进程,包括重启崩溃或异常退出的子进程,同时也响应来自客户端的请求。

supervisorctl:supervisord服务的客户端命令行。获得由主进程控制的子进程的状态,停止和启动子进程,并获得主进程的运行列表。

Web Server:通过web界面查看和控制进程状态。

XML-RPC Interface:服务于web UI的同一个HTTP服务器提供一个XML-RPC接口,可以用来询问和控制管理程序及其运行的程序。

配置文件详解

[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 ;日志文件
logfile_maxbytes=50MB        ;日志文件大小
logfile_backups=10           ;日志文件保留备份数量
loglevel=info                ;日志级别,默认info
pidfile=/tmp/supervisord.pid ;pid 文件
nodaemon=false               ;是否在前台启动,默认是false
minfds=1024                  ;可以打开的文件描述符的最小值
minprocs=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秒后没有异常退出
autorestart=true     ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3       ; 启动失败自动重试次数
user=tomcat          ; 用哪个用户启动进程
priority=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 =/etc/supervisord.d/*.ini    ;可以指定一个或多个以.ini结束的配置文件

安装方式

1.源码安装

# 下载源码包
wget https://pypi.python.org/packages/7b/17/88adf8cb25f80e2bc0d18e094fcd7ab300632ea00b601cbbbb84c2419eae/supervisor-3.3.4.tar.gz

# 解压
tar -zxvf supervisor-3.3.4.tar.gz

# 进入supervisor-3.3.4
cd supervisor-3.3.4

# 下载
python setup.py install 

2.yum 安装

yum install supervisor -y

启动服务

[root@master ~]# service supervisord start
Redirecting to /bin/systemctl start supervisord.service

# 看进程
[root@master ~]# ps aux|grep supervisord
root      17082  0.0  0.6 221476 11880 ?        Ss   00:02   0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root      17084  0.0  0.0 112824   988 pts/0    S+   00:02   0:00 grep --color=auto supervisord

编辑配置文件,配置web页面访问

# 去掉注释(;)
[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)
username=user              ; (default is no username (open server))
password=123               ; (default is no password (open server))

[root@master ~]# vim /etc/supervisord.conf 
[inet_http_server]         ; inet (TCP) server disabled by default
port=192.168.1.120:9001        ; (ip_address:port specifier, *:port for all iface)
username=user              ; (default is no username (open server))
password=123               ; (default is no password (open server))

# 重启服务
[root@master ~]# service supervisord restart
Redirecting to /bin/systemctl restart supervisord.service

[root@master ~]# ps -aux|grep supervisord
root      17112  0.0  0.6 221480 11904 ?        Ss   00:05   0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root      17120  0.0  0.0 112824   988 pts/0    S+   00:06   0:00 grep --color=auto supervisord


浏览器上访问:http://192.168.1.120:9001/

 创建守护nginx进程配置文件 nginx.ini

[root@master ~]# cd /etc/supervisord.d/
[root@master supervisord.d]# vim nginx.ini
[root@master supervisord.d]# cat nginx.ini 
[program:nginx]
command=/usr/./sbin/nginx -g 'daemon off;'  user=root                                               
autostart=false                                       
autorestart=true                                     
stopasgroup=true                                        
killasgroup=true                                        
stdout_logfile=/etc/supervisord.d/nginx-out.log         
stderr_logfile=/etc/supervisord.d/nginx-err.log


# 重启服务
[root@master supervisord.d]# service supervisord restart
Redirecting to /bin/systemctl restart supervisord.service

[root@master supervisord.d]# ps aux|grep supervisord
root      17148  0.0  0.6 221764 11916 ?        Ss   00:13   0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root      17150  0.0  0.0 112824   988 pts/0    S+   00:13   0:00 grep --color=auto supervisord

下载nginx

yum install nginx -y

使用Supervisor 进行管理

supervisorctl status                    # 状态
supervisorctl stop nginx                #关闭 nginx
supervisorctl start nginx               #启动 nginx
supervisorctl restart nginx             #重启 nginx
supervisorctl reload                    #重启全部
supervisorctl update                    #更新配置
[root@master supervisord.d]# supervisorctl status
nginx                            STOPPED   Not started

[root@master supervisord.d]# supervisorctl start nginx
nginx: ERROR (spawn error)

报错:ERROR (spawn error)

看日志

[root@master supervisord.d]# tail -f  /var/log/supervisor/supervisord.log
2023-08-08 00:13:44,662 INFO supervisord started with pid 17148
2023-08-08 00:17:04,233 INFO spawned: 'nginx' with pid 17191
2023-08-08 00:17:04,249 INFO exited: nginx (exit status 1; not expected)
2023-08-08 00:17:05,258 INFO spawned: 'nginx' with pid 17192
2023-08-08 00:17:05,278 INFO exited: nginx (exit status 1; not expected)
2023-08-08 00:17:07,292 INFO spawned: 'nginx' with pid 17193
2023-08-08 00:17:07,314 INFO exited: nginx (exit status 1; not expected)
2023-08-08 00:17:10,334 INFO spawned: 'nginx' with pid 17194
2023-08-08 00:17:10,340 INFO exited: nginx (exit status 1; not expected)
2023-08-08 00:17:10,340 INFO gave up: nginx entered FATAL state, too many start retries too quickly


[root@master supervisord.d]# cat nginx-err.log 
nginx: invalid option: "user=root"
nginx: invalid option: "user=root"
nginx: invalid option: "user=root"
nginx: invalid option: "user=root"

 解决

[root@master supervisord.d]# cat nginx.ini 
[program:nginx]
command=/usr/./sbin/nginx -g 'daemon off;'  
user=root                                               
autostart=false                                       
autorestart=true                                     
stopasgroup=true                                        
killasgroup=true                                        
stdout_logfile=/etc/supervisord.d/nginx-out.log         
stderr_logfile=/etc/supervisord.d/nginx-err.log

[root@master supervisord.d]# service supervisord restart
Redirecting to /bin/systemctl restart supervisord.service

[root@master supervisord.d]# supervisorctl status
nginx                            STOPPED   Not started

[root@master supervisord.d]# supervisorctl start nginx
nginx: started

[root@master supervisord.d]# supervisorctl status
nginx                            STARTING  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩未零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值