supervisor使用详解

Supervisor是一个用Python编写的进程管理工具,用于在Linux系统中监听、启动、停止和重启进程。它可以自动恢复意外终止的进程。文章详细介绍了通过pip和yum两种方式安装Supervisor,配置主配置文件和子配置文件来监控特定进程,以及如何使用命令行工具supervisorctl来管理进程的状态和操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 介绍

使用文档:Supervisor: A Process Control System — Supervisor 4.2.5 documentation
supervisor是Python开发的c/s服务,是Linux系统下的进程管理工具。
可以监听、启动、停止、重启一个或多个进程
用supervisor管理的进程,当一个进程意外被杀死,supervisor监听到进程死后,会自动将它重启,很方便的做到进程的自动恢复的功能,不在需要自己写脚本来控制

二 安装

2.1 pip安装

# pip install supervisor

#生成配置文件
sudo mkdir /etc/supervisor
# echo_supervisord_conf > /etc/supervisor/supervisord.conf

2.2 使用yum进行安装

# yum install -y epel-release
# yum install -y supervisor

三 配置文件

使用yum安装的supervisor会在/etc/会生成一个supervisor.conf文件及一个supervisor.d文件目录

3.1 主配置文件

[root@test-Server1 supervisord.d]# grep -v "^;" /etc/supervisord.conf |grep -v "^$"
[unix_http_server]
file=/run/supervisor/supervisor.sock   ; (the path to the socket file)
[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=123abc             ; (default is no password (open server))
[supervisord]
logfile=/var/log/supervisor/supervisord.log  ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (log level;default info; others: debug,warn,trace)
pidfile=/run/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:///run/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket
[include]
files = supervisord.d/*.conf

3.2 配置子配置文件用于监控各个进程

[root@test-Server1 supervisord.d]# cat supervisor_java_one.conf
[program:thrk_erp]
command=/usr/local/java/bin/java -jar /www/wwwroot/renkang-erp/thrk_erp-1.0-SNAPSHOT.jar  --server.port=9527 --spring.profiles.active=self >> /mnt/xinceshi/erp.log
priority=1                    ; 数字越高,优先级越高
numprocs=1                    ; 启动几个进程
autostart=true                ; 随着supervisord的启动而启动
autorestart=true              ; 自动重启
startretries=10               ; 启动失败时的最多重试次数
exitcodes=0                   ; 正常退出代码
stopsignal=KILL               ; 用来杀死进程的信号
stopwaitsecs=10               ; 发送SIGKILL前的等待时间
redirect_stderr=true          ; 重定向stderr到stdout

[program:userservice]
command=/usr/local/java/bin/java -jar  /www/wwwroot/renkang-state/usersevice-1.0-SNAPSHOT.jar --server.port=19999 --spring.profiles.active=self 
priority=1                    ; 数字越高,优先级越高
numprocs=1                    ; 启动几个进程
autostart=false               ; true随着supervisord的启动而启动,即使进程存在,它也会重新启动一个,false就不会重新启动了
autorestart=true              ; 自动重启
startretries=10               ; 启动失败时的最多重试次数
exitcodes=0                   ; 正常退出代码
stopsignal=KILL               ; 用来杀死进程的信号
stopwaitsecs=10               ; 发送SIGKILL前的等待时间
redirect_stderr=true          ; 重定向stderr到stdout

四 启动supervisord

supervisord -c /etc/supervisord.conf
 systemctl start supervisord.service

五 supervisor常用命令

supervisorctl status   # 查看进程运行状态
supervisorctl start weather   # 启动进程
supervisorctl stop weather    # 关闭进程
supervisorctl restart weather   # 重启进程
supervisorctl update    # 重新载入配置文件
supervisorctl shutdown    # 关闭 supervisord
supervisorctl clear weather    # 清空进程日志
supervisorctl stop restart + all    # 表示启动,关闭,重启所有进程。

我想启动上面配置好的任务就是supervisorctl start userservice

image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值