Supervisor 进程管理工具


简介:

Supervisor 进程管理工具

一、安装

shell > yum -y install python-pip

shell > pip install supervisor

# 这样就安装好了,注意:这货不支持 Python 3、用 yum 安装也有问题

二、配置

shell > echo_supervisord_conf > /etc/supervisord.conf

# 生成配置文件到指定位置,报错的时候卸载原来的包装这个 meld3==0.6.7 [ pkg_resources.DistributionNotFound: meld3>=0.6.5 ]

shell > grep -vP '^;|^$' /etc/supervisord.conf 

[unix_http_server]
file=/var/tmp/supervisor.sock      ; .sock 存放位置

[supervisord]
logfile=/var/log/supervisord.log   ; .log  存放位置
logfile_maxbytes=50MB              ; 每个日志文件最大 50MB
logfile_backups=10                 ; 保留10个备份
loglevel=info                      ; 日志级别,info,debug,warn,trace
pidfile=/var/run/supervisord.pid   ; .pid  存放位置
nodaemon=false                     ; 守护进程方式启动
minfds=1024                        ; 可以打开的文件描述符
minprocs=200                       ; 可以启动的进程数

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/tmp/supervisor.sock  ; .sock 存放位置

# 稍微修改了一些默认项

三、启动

shell > supervisord -c /etc/supervisord.conf

# 指定配置文件路径

shell > ps aux | grep sup
root        40  0.0  0.0      0     0 ?        S    15:49   0:00 [sync_supers]
root      4044  0.0  0.1 199380 11164 ?        Ss   17:33   0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf

四、启动一个自定义脚本

shell > vim hello.sh

#!/bin/bash

for i in {1..10};do echo hello; sleep 1;done

shell > chmod a+x hello.sh
shell > vim /etc/supervisord.conf

[program:hello]
directory=/root               ; 运行程序时切换到指定目录
command=/bin/bash hello.sh    ; 执行程序 ( 程序不能时后台运行的方式 )
redirect_stderr=true          ; 标准错误输出重定向到标准输出
stdout_logfile=hello.log      ; 指定日志文件路径,可以绝对路径 ( 相对路径 相对 directory= 指定的目录 )
stdout_logfile_maxbytes=50MB  ; 文件切割大小
stdout_logfile_backups=10     ; 保留的备份数

# 加入程序启动配置
shell > supervisorctl

supervisor> help

default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail   
avail  fg        pid   remove  shutdown  status  update 
clear  maintail  quit  reread  signal    stop    version

# 客户端工具提供一些指令 ( supervisorctl help 这样运行也是可以的 )

supervisor> update
hello: added process group

# 自动重启配置文件发生改变的进程

supervisor> start hello
hello: started

supervisor> status
hello                            RUNNING   pid 4125, uptime 0:00:04

# 反正就是这几个指令,启动后脚本的输出会被记录到 stdout_logfile= 指定的日志文件中

# 现在的情况是,程序执行一次就退出了

supervisor> status
hello                            EXITED    Dec 01 06:02 PM

supervisor> exit
shell > vim /etc/supervisord.conf

[program:hello]
directory=/root
command=/bin/bash hello.sh
autostart=true                ; 程序随 supervisord 启动而启动
startsecs=10                  ; 程序启动 10 后没有退出,认为程序启动成功 
startretries=3                ; 启动失败重试次数
autorestart=true              ; 程序退出后自动启动,false 不启动、unexpected 只有退出状态码为 exitcodes= 指定的值是才自动启动
redirect_stderr=true
stdout_logfile=hello.log
stdout_logfile_maxbytes=50MB  
stdout_logfile_backups=10

# 添加一些配置,重新来过

supervisor> update
hello: stopped
hello: updated process group

# 注意:这次会时间长一点,因为设置了 startsecs=10,也就是说 10 秒后才能 status 看到进程状态

# 这次程序就会自动重启了
shell > vim /etc/supervisord.conf

[program:hello]
directory=/root
command=/bin/bash hello.sh
process_name=%(program_name)s_%(process_num)02d  ; 启动多个进程时设置不同的进程名
numprocs=2                                       ; 启动几个进程
autostart=true
startsecs=5
startretries=3
autorestart=true
redirect_stderr=true
stdout_logfile=hello.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10

# 又加了启动多个进程的配置

supervisor> update
hello: stopped
hello: updated process group

supervisor> status
hello:hello_00                   RUNNING   pid 4899, uptime 0:00:09
hello:hello_01                   RUNNING   pid 4898, uptime 0:00:09

# 这次长这样了..

supervisor> stop all
hello:hello_00: stopped
hello:hello_01: stopped

supervisor> status
hello:hello_00                   STOPPED   Dec 01 06:27 PM
hello:hello_01                   STOPPED   Dec 01 06:27 PM

# 好了收工!

转载于:https://www.cnblogs.com/wangxiaoqiangs/p/7943979.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值