重要的进程就让Supervisor 来守护吧

Supervisor 介绍:

Supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

 

Supervisor 安装:

复制代码
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc* make -y
yum install python
-setuptools

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple supervisor

  echo_supervisord_conf > /etc/supervisord.conf

或 vim /etc/supervisord.conf

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)

[supervisord]
logfile=/app/logs/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/app/logs/supervisor ; ('AUTO' child log dir, default $TEMP)
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket

; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.

[include]
files = /etc/supervisord/conf.d/*.ini

 

 

复制代码

 supervisor 常用命令

复制代码
supervisord  -c /etc/supervisord.conf   supervisord后台启动
supervisorctl  shutdown             关闭supervisord
supervisord -v                      查看版本
ps -elf | grep supervisor

supervisorctl start program_name     启动某个进程
supervisorctl stop program_name         停止某个进程
supervisorctl restart program_name     重启某个进程
supervisorctl status program_name     查看某个进程的状态
supervisorctl stop all                 停止全部进程
supervisorctl reload                 载入最新的配置文件,重启所有进程
supervisorctl update                 根据最新的配置,重启配置更改过的进程,未更新的进程不受影响
复制代码

编写需要守护的程序

复制代码
vim /etc/supervisor/config.d/web-admin.ini

[program:web-admin]
command=/data/web-admin/start.sh    ;程序启动命令
directory=/data/web-admin           ;启动目录
autostart=true                      ;在supervisord启动的时候也自动启动
startsecs=10                        ;启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true                    ;程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3                      ;启动失败自动重试次数,默认是3
user=root                           ;命令执行的用户 这里设置出错会导致权限问题,默认是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=/var/logs/web-admin.log
; stopasgroup=false ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
; killasgroup=false ;默认为false,向进程组发送kill信号,包括子进程
复制代码

 

supervisorctl reload   

 

posted @   zyl88  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示