avatar

麦兜的小站

MDO.INK

  • 首页
  • 随笔
  • 知识库
  • 归档
  • 动态
  • 标签
  • 关于
Home supervisor 安装、配置、常用命令
文章

supervisor 安装、配置、常用命令

Posted 2025-01-24 Updated 2025-01- 24
By power 已删除用户
6~8 min read

在 web 应用部署到线上后,需要保证应用一直处于运行状态,在遇到程序异常、报错等情况,导致 web 应用终止时,需要保证程序可以立刻重启,继续提供服务。

所以,就需要一个工具,时刻监控 web 应用的运行情况,管理该进程。

Supervisor 就是解决这种需求的工具,可以保证程序崩溃后,重新把程序启动起来等功能。

简介

Supervisor 是一个用 Python 写的进程管理工具,可以很方便的用来在 UNIX-like 系统(不支持 Windows)下启动、重启(自动重启程序)、关闭进程(不仅仅是 Python 进程)。

安装

  1. Ubuntu系统下:apt-get install supervisor,通过这种方式安装后,自动设置为开机启动
  2. 也可以通过 pip install supervisor 进行安装,但是需要手动启动,然后设置为开机启动(不推荐这种安装方式)

Supervisor 配置

Supervisor 是一个 C/S 模型的程序,supervisord 是 server 端,supervisorctl 是 client 端。

supervisord

下面介绍 supervisord 配置方法。supervisord 的配置文件默认位于 /etc/supervisord.conf,内容如下(;后面为注释):

[unix_http_server]
file=/var/run/supervisor.sock   
chmod=0700                       

[supervisord]
logfile=/var/log/supervisor/supervisord.log 
pidfile=/var/run/supervisord.pid 
childlogdir=/var/log/supervisor            




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

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock 



[include]
files = /etc/supervisor/conf.d/*.conf 

program 配置

program 的配置文件就写在,supervisord 配置中 include 项的路径下:/etc/supervisor/conf.d/,然后 program 的配置文件命名规则推荐:app_name.conf

[program:app] 
autorestart=True      
autostart=True        
redirect_stderr=True  
environment=PATH="/home/app_env/bin"  
command=python server.py  
user=ubuntu           
directory=/home/app/  
stdout_logfile_maxbytes = 20MB  
stdout_logfile_backups = 20     

stdout_logfile = /data/logs/usercenter_stdout.log

需要注意:

  • 用 supervisord 管理时,gunicorn 的 daemon 选项需要设置为 False
  • 如果启动命令需要包含workon,修改environment参数:environment=PATH="/home/username/.virtualenvs/myproject/bin"

supervisorctl 操作

supervisorctl 是 supervisord 的命令行客户端工具,使用的配置和 supervisord 一样,这里就不再说了。下面,主要介绍 supervisorctl 操作的常用命令:

输入命令 supervisorctl 进入 supervisorctl 的 shell 交互界面(还是纯命令行😓),就可以在下面输入命令了。:

  • help # 查看帮助
  • status # 查看程序状态
  • stop program_name # 关闭 指定的程序
  • start program_name # 启动 指定的程序
  • restart program_name # 重启 指定的程序
  • tail -f program_name # 查看 该程序的日志
  • update # 重启配置文件修改过的程序(修改了配置,通过这个命令加载新的配置)

也可以直接通过 shell 命令操作:

  • supervisorctl status
  • supervisorctl update
  • …
知识库
License:  CC BY 4.0
Share

Further Reading

Jul 31, 2025

如何实现接口幂等性

通俗的说,用户在系统中有操作,不管重复多少次,都应该产生一样的效果或返回一样的结果的。 幂等性的概念 幂等(Idempotent)是一个数学与计算机学的概念,常见于抽象代数中。 f(n)=1^n//无...

Jul 19, 2025

10个npm工具包

有了npm之后,前端人员真的是过上好日子了。我们可以直接把别人写好的工具包拿来用,非常的方便。 1.day.js-轻量日期处理 npminstalldayjs importdayjsfrom'd...

Jul 17, 2025

How to set up PHP7.4 on MacOS.

Thisisallverywellandgood.Apartfromonesmallinsignificantthing… TheversionofPHPinuseiscurrently7.4. Th...

OLDER

调用API保存并发布文章java示例

NEWER

PM2 - Quick Start

Recently Updated

  • 如何实现接口幂等性
  • 10个npm工具包
  • How to set up PHP7.4 on MacOS.
  • Automa:一键自动化,网页数据采集与工作流程优化专家Automa:解锁自动化
  • Mac 下用 brew 搭建 LNMP

Trending Tags

thinkphp clippings

Contents

©2025 麦兜的小站. Some rights reserved.

Using the Halo theme Chirpy