当前位置:网站首页>supervisor 使用文档
supervisor 使用文档
2022-07-06 07:04:00 【afterlife_union】
1、supervisor 解决什么问题
我们的每台服务器都可能需要运行好几个进程,比如一个服务器往往同时存在nginx、php-fpm多个进程,对于执行定时任务的机器则往往会同时运行多个cli进程任务,如果使用命令行方式一个一个去手动启动和停止显然非常麻烦、低效,而且任务如果异常退出、每次都靠人工去重启也不现实,另外查看每个进程的状态也很不方便。Supervisord工具就是用来管理每台机器上多个进程的,与Supervisord类似的工具包括monit, daemontools和runit。
2、supervisor介绍
总结来说,Supervisor 是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,自动重启异常退出的进程,同时提供了命令行程序和web界面用于查看、管理进程。
Supervisor在大多数类Unix系统都可以正常工作,包括Linux,Mac OS X 和Solaris(10 for Intel)和FreeBSD 6.1,但是不支持 Windows平台。
网上一些资料显示Supervisor 只能在Python 2下运行,但实际上最新版本的Supervisor已经支持Python 3,要求>= Python 3.4。
官方网站
http://supervisord.org/
2、supervisor安装
mac 安装命令如下
brew install supervisor
启动supervisor
brew services restart supervisor
从启动supervisor的输出可以看到,使用的配置文件如下
/opt/homebrew/etc/supervisord.conf
该配置文件显示,会从 /opt/homebrew/etc/supervisor.d/ 中读取 .ini配置文件,管理相关的进程任务
使用-c参数可以指定主配置文件
/opt/homebrew/bin/supervisord -c /opt/homebrew/etc/supervisord.conf
关闭、重启命令
关闭supervisor
brew services stop supervisor
3、supervisor配置
配置可以分成两类 supervisord.conf 配置 和 子进程配置
3.1 supervisord.conf 配置
[unix_http_server]
file=/tmp/supervisor.sock #UNIX socket 文件,supervisorctl 会使用
chmod=0700 #socket文件的mode,默认是0700
chown=nobody:nogroup #socket文件的owner,格式:uid:gid
username=user # 认证账户
password=123 # 认证密码
[inet_http_server] #HTTP服务器,提供web管理界面,默认未开启,启用后可以通过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
#包含其它配置文件
[include]
files = relative/directory/*.ini #可以指定一个或多个以.ini结束的配置文件
3.2 program配置
也就是需要监控的子进程配置,
# 子进程的名称,也即web界面中展示的进程名称
[program:blog]
#脚本目录
directory=/opt/bin
#脚本执行命令
command=/usr/bin/python /opt/bin/test.py
#supervisor启动的时候是否随着同时启动,默认True
autostart=true
#当程序exit的时候,这个program不会自动重启,默认unexpected,设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。如果为false的时候,无论什么情况下,都不会被重新启动,如果为unexpected,只有当进程的退出码不在下面的exitcodes里面定义的
autorestart=false
#这个选项是子进程启动多少秒之后,此时状态如果是running,则我们认为启动成功了。默认值为1
startsecs=1
#脚本运行的用户身份
user = test
#日志输出
stderr_logfile=/tmp/blog_stderr.log
stdout_logfile=/tmp/blog_stdout.log
#把stderr重定向到stdout,默认 false
redirect_stderr = true
#stdout日志文件大小,默认 50MB
stdout_logfile_maxbytes = 20MB
#stdout日志文件备份数
stdout_logfile_backups = 20
4、supervisorctl命令
命令示例如下
supervisorctl status //查看所有进程的状态
supervisorctl stop es //停止es
supervisorctl start es //启动es
supervisorctl restart //重启es
supervisorctl update //配置文件修改后使用该命令加载新的配置
supervisorctl reload //重新启动配置中的所有程序
注:把es换成all可以管理配置中的所有进程。直接输入supervisorctl进入supervisorctl的shell交互界面,此时上面的命令不带supervisorctl可直接使用。
5、数据聚合
如果有大量服务需要管理,怎么把这些机器的supervisor所管理的服务统一管理?
6、问题
6.1 supervisor couldn’t setuid to 0 Can’t drop privilege as nonroot user
主配置文件和子进程的配置文件的用户都是用的相同的root用户,通过下面的命令启动时
/opt/homebrew/bin/supervisord -c /opt/homebrew/etc/supervisord.conf
一直报下面的错误
Error: Can't drop privilege as nonroot user
For help, use /opt/homebrew/bin/supervisord -h
查找资料提示,说是以root用户启动supervisor,但是以非root用户启动
https://stackoverflow.com/questions/67344737/error-cant-drop-privilege-as-nonroot-user-container-keeps-restarting-on-googl
于是想到以root 用户执行启动命令,命令如下,最终程序可以正常运行。
sudo /opt/homebrew/bin/supervisord -c /opt/homebrew/etc/supervisord.conf
6.2 启动命令
supervisor主配置和子进程的配置文件都支持设置执行用户,但是似乎如果用户不同的话,是启动不起来的,不确定这是不是必须的要求
参考文档
https://zhuanlan.zhihu.com/p/424346764
https://www.jianshu.com/p/00fbff2c6ee1
https://blog.csdn.net/cyt0906/article/details/107344361
边栏推荐
- NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
- Establishment and operation of cloud platform open source project environment
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
- Automated test environment configuration
- 将ue4程序嵌入qt界面显示
- 软件测试外包到底要不要去?三年真实外包感受告诉你
- Zhongqing reading news
- [some special grammars about C]
- How to reconstruct the class explosion caused by m*n strategies?
- What is the difference between int (1) and int (10)? Senior developers can't tell!
猜你喜欢

UWA Pipeline 2.2.1 版本更新说明

Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案

leetcode1020. 飞地的数量(中等)

Basic commands of MySQL

The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module

Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple

Misc of BUU (update from time to time)

【每日一题】729. 我的日程安排表 I

Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin

18.多级页表与快表
随机推荐
Leetcode daily question (1870. minimum speed to arrive on time)
PCL实现选框裁剪点云
[some special grammars about C]
Three methods of adding color to latex text
这个高颜值的开源第三方网易云音乐播放器你值得拥有
接口自动化测试框架:Pytest+Allure+Excel
UDP攻击是什么意思?UDP攻击防范措施
pymongo获取一列数据
微信脑力比拼答题小程序_支持流量主带最新题库文件
Chapter 7 - thread pool of shared model
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
leetcode59. 螺旋矩阵 II(中等)
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
Top test sharing: if you want to change careers, you must consider these issues clearly!
Raspberry pie serial port login and SSH login methods
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
18.多级页表与快表
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module