惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - uestc2007

AI 智能体项目的测试 vllm方式部署Deepseek R1、Embedding、Reranker、Qwen 磁盘挂载 LibreOffice 批量将.doc文件转换为.docx 容器异常或停止自动启动脚本 资源监控脚本 Docker 安全及日志管理 Docker资源控制 做项目管理需要哪些技能 项目管理的基本工作是什么 知识图谱介绍(三) 知识图谱介绍(二) 知识图谱介绍(一) 安装Kuboard管理k8s Registry&Harbor私有仓库构建 EMQX配置用户名和密码开启emqx_auth_mnesia认证方式连接 开关量、数字量、模拟量、离散量和脉冲量它们之间有什么区别? Kubernetes基础总结 Kubernetes管理应用程序、服务常用命令、集群监视
Supervisor 监控 Python 服务
uestc2007 · 2025-12-17 · via 博客园 - uestc2007

使用 Supervisor 来监控并自动重启 Python 应用。以下是配置步骤:

1. 安装 Supervisor

#ubuntu
sudo apt update
sudo apt install supervisor

#centos
sudo yum update
sudo yum install supervisor

2. 创建 Supervisor 配置文件

在 /etc/supervisor/conf.d/ 目录下创建一个配置文件(例如 kg.conf):

vi /etc/supervisor/conf.d/kg.conf,将以下内容写入配置文件:

[program:kg_ai_service2]
command=python3 /home/iot/wh/kg/run.py  ; 运行命令
directory=/home/iot/wh/kg               ; 工作目录
user=iot                                            ; 运行用户
autostart=true                                      ; 是否随 Supervisor 启动
autorestart=true                                    ; 自动重启
startretries=3                                      ; 启动失败时的重试次数
startsecs=10                                        ; 启动 10 秒后无异常则视为正常
stderr_logfile=/var/log/kg_err.log      ; 错误日志路径
stdout_logfile=/var/log/kg_out.log      ; 输出日志路径
environment=PYTHONPATH="/home/iot/wh/kg" ; 环境变量(可选)

方法1:直接使用虚拟环境的 Python 路径

[program:kg]
command=/root/python/uie/bin/python /home/iot/wh/kg/run.py
directory=/home/iot/wh/kg
user=root  # 如果虚拟环境在 /root 目录下,可能需要使用 root 用户
autostart=true
autorestart=true
startretries=3
startsecs=10
stderr_logfile=/var/log/kg_err.log
stdout_logfile=/var/log/kg_out.log
environment=PYTHONPATH="/home/iot/wh/kg"

方法2:使用 bash -c 激活虚拟环境


[program:kg]
command=bash -c 'source /root/python/uie/bin/activate && python /root/wh/kg/run.py'
directory=/root/wh/kg
user=root
autostart=true
autorestart=true
startretries=3
startsecs=10
stopwaitsecs=60
stopsignal=TERM
stdout_logfile=/var/log/supervisor/kg_out.log
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=10
stderr_logfile=/var/log/supervisor/kg_err.log
stderr_logfile_maxbytes=10MB
stderr_logfile_backups=10
environment=
PYTHONPATH="/root/wh/kg:/root/python/uie/lib/python3.X/site-packages",PATH="/root/python/uie/bin:%(ENV_PATH)s"

方法3:使用虚拟环境中的 python 并添加路径

[program:kg_ai_service2]
command=/root/python/uie/bin/python /home/iot/wh/kg_ai_service2/run.py
directory=/home/iot/wh/kg_ai_service2
user=root
autostart=true
autorestart=true
startretries=3
startsecs=10
stderr_logfile=/var/log/kg_ai_service2.err.log
stdout_logfile=/var/log/kg_ai_service2.out.log
environment=HOME="/root",PYTHONPATH="/home/iot/wh/kg_ai_service2",PATH="/root/python/uie/bin:%(ENV_PATH)s"

3. 重新加载 Supervisor 配置

# 更新 Supervisor 配置
sudo supervisorctl reread
sudo supervisorctl update

# 启动服务
sudo supervisorctl start kg

# 查看服务状态
sudo supervisorctl status kg

# 查看服务日志
sudo tail -f /var/log/kg_out.log

4. 管理应用

# 查看所有服务状态
sudo supervisorctl status

# 重启服务
sudo supervisorctl restart kg

# 停止服务
sudo supervisorctl stop kg

# 重新启动所有服务
sudo supervisorctl reload

# 查看 Supervisor 日志
sudo tail -f tail -f /var/log/kg_out.log