Mac环境安装和使用supervisor
创始人
2024-04-01 08:25:44
0

Mac环境安装和使用 supervisor

supervisor 用于管理自定义任务进程,可配置服务自动重启等多种功能,方便用户的计划任务管理。

安装

推荐采用 brew 包管理工具进行安装:

brew install supervisor

如果通过pip进行安装,需要重点关注pip所在的python环境,是处于本机系统下还是在conda env中。
不同环境下,supervisor的配置文件位置和参数有差异,容易导致问题。

检查是否安装成功:

% brew info supervisor
==> supervisor: stable 4.2.4 (bottled), HEAD
Process Control System
http://supervisord.org/
/opt/homebrew/Cellar/supervisor/4.2.4 (765 files, 10.6MB) *Poured from bottle on 2022-10-28 at 19:42:31
From: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git/Formula/supervisor.rb
License: BSD-3-Clause-Modification
==> Dependencies
Required: python@3.10 ✔
==> Options
--HEADInstall HEAD version
==> Caveats
To restart supervisor after an upgrade:brew services restart supervisor
Or, if you don't want/need a background service you can just run:/opt/homebrew/opt/supervisor/bin/supervisord -c /opt/homebrew/etc/supervisord.conf --nodaemon
==> Analytics
install: 640 (30 days), 2,119 (90 days), 15,378 (365 days)
install-on-request: 638 (30 days), 2,117 (90 days), 15,384 (365 days)
build-error: 0 (30 days)

其默认配置文件位置:/opt/homebrew/etc/supervisord.conf

配置

这里首先说明一下,有不少文章说明是按照如下方式生成supervisord配置文件:

echo_supervisord_conf > /usr/local/etc/supervisord.ini

但是本文实践过程中,不需要这一步骤,安装完成后即自动存在supervisord.conf配置文件。
接下来,我们直接打开并修改配置文件。

启用http服务

打开监听TCP socket,启动inet的http服务。这个模块默认是未开启状态,打开后可通过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)

查看 supervisord全局配置

[supervisord]部分,可看到logfile等各类配置参数,一般情况下保持默认即可。

修改 supervisorctl配置

supervisord 与 supervisorctl 之间是服务端与客户端的关系,因此,需要配置如下:

[supervisorctl]
;serverurl=unix:///opt/homebrew/var/run/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
username=user                ; should be same as in [*_http_server] if set
password=123                 ; should be same as in [*_http_server] if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

这里服务端url和账户信息,要与前面 [inet_http_server]的配置保持一致。

编写 program任务配置

supervisord.conf的结尾处,定义了如何管理自定义配置文件:

; 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 = /opt/homebrew/etc/supervisor.d/*.ini

因此,不推荐直接在 supervisord.conf中编写 program,而应该按照如上include说明,在同级目录下创建一个文件夹 supervisor.d,然后将自己的任务配置文件放在该文件夹下。

子进程配置项主要包括:

  • program: 设置子进程的名称,也即web界面中展示的进程名称,不允许重复。
  • command: 启动子进程的命令行
  • directory: 执行的项目目录
  • stdout_logfile: log日志输出文件路径,必须事先创建好

配置示例:

[program:frpc]
command=/Users/username/works/my_program.sh
directory=/Users/username/works
autostart=false
startsecs=5
stdout_logfile=/Users/username/works/my_program.log

开始运行

启动 supervisor

supervisord -c supervisord.conf

进入 web后台管理页面,浏览器输入:http://127.0.0.1:9001,按照提示输入前面配置的用户名密码。
登录成功后,即可通过web界面来操作已经配置的子进程。
在这里插入图片描述

命令行管理

除了通过web页面进行任务进程的管理,也可使用 supervisorctl 命令进行管理。
常用命令如下:

#关闭所有任务
supervisorctl shutdown 
# 启动某个进程
supervisorctl start programxxx
# 重启某个进程
supervisorctl restart programxxx
# 停止全部进程 注:start、restart、stop都不会载入最新的配置文件
supervisorctl stop all
# 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。
supervisorctl reload
# 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。
supervisorctl update

参考资料

supervisor安装与使用教程for Mac

相关内容

热门资讯

Adobe 因使用 SlimP... AIPress.com.cn报道 12月29日消息,作为全球创意软件巨头,Adobe 正面临其首起重...
伟星新材:竞争优势明显 保持积... 12月28日,伟星新材(002372)发布公告,伟星新材(002372)于2025年12月25日召开...
健全数据制度 释放乘数效应——... 来源:经济日报 党的二十届四中全会审议通过的《中共中央关于制定国民经济和社会发展第十五个五年规划的建...
海关出口退税律师张严锋:套用其... 2018年3月2日,B稽查局对A公司涉税事项进行检查。经检查,B稽查局认为A公司涉嫌通过套用他人出口...
哈尔滨权威刑事律师服务推荐:谷... 在哈尔滨,当人们遭遇刑事法律问题时,往往会困惑于刑事律师服务哪家权威刑事律师推荐哪些刑事辩护律师哪个...
原创 《... 2025年12月26日,《晋中市平遥牛肉保护和发展条例》新闻发布会在晋中举行。该条例经山西省人大常委...
权威非法吸收公共存款律师推荐:... 在处理非法吸收公共存款这类复杂刑事案件时,选择一位经验丰富、靠谱且性价比高的律师至关重要。今天,我们...
政策领航聚英才 智农创新兴龙江 □刘凌灼 本报记者 姜斌 刘畅 在广袤的北大荒黑土地上,一片农业科技创新的“新沃土”正破土而出。这里...
上海海关商品归类律师张严锋:自... 自动衬环成型绕圆机如何归类 2023年01月01日至2024年05月30日期间,A公司以一般贸易方...
原创 男... 男子举报自己无证驾驶:进去待两天 朋友们,我最近看到一个很火的帖子,真的让我很感动!“男子自首承认无...