在 CentOS 上安装 Supervisor 并配置守护这两个 Python 进程的详细步骤,包括日志设置。
CentOS 默认不包含 Supervisor,需要通过以下步骤安装:
# 安装 EPEL 仓库(提供 Supervisor 软件包)
sudo yum install -y epel-release
# 安装 Supervisor
sudo yum install -y supervisor
确保 Supervisor 开机自启并启动服务:
# 启动 Supervisor 服务
sudo systemctl start supervisord
# 设置 Supervisor 开机自启
sudo systemctl enable supervisord
Supervisor 的配置文件通常位于 /etc/supervisord.conf
或 /etc/supervisord.d/*.conf
。我们将为两个 Python 进程创建配置文件。
确保日志目录存在:
mkdir -p /home/python/tool/plat/logs
在 /etc/supervisord.d/
目录下创建配置文件,例如 plat_processes.ini
:
sudo mkdir -p /etc/supervisord.d
sudo nano /etc/supervisord.d/plat_processes.ini
添加以下内容:
[program:httprelease]
command=python3 /home/python/tool/plat/httprelease.py
directory=/home/python/tool/plat
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=10
stdout_logfile=/home/python/tool/plat/logs/httprelease.log
stderr_logfile=/home/python/tool/plat/logs/httprelease_error.log
user=root
environment=PYTHONPATH="/home/python/tool/plat"
[program:http_up_release]
command=python3 /home/python/tool/plat/http_up_release.py
directory=/home/python/tool/plat
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=10
stdout_logfile=/home/python/tool/plat/logs/http_up_release.log
stderr_logfile=/home/python/tool/plat/logs/http_up_release_error.log
user=root
environment=PYTHONPATH="/home/python/tool/plat"
说明:
command
:指定运行的命令。directory
:进程运行的工作目录。autostart
:Supervisor 启动时自动启动进程。autorestart
:进程异常退出时自动重启。stdout_logfile
和 stderr_logfile
:分别指定标准输出和错误日志的路径。user
:运行进程的用户(根据实际需求可修改为其他用户)。environment
:设置环境变量,确保 Python 脚本能找到依赖。确保日志目录和文件的权限正确:
sudo chown -R root:root /home/python/tool/plat/logs
sudo chmod -R 755 /home/python/tool/plat/logs
使配置生效:
# 重新加载配置
sudo supervisorctl reread
sudo supervisorctl update
启动、停止或检查进程状态:
# 启动所有进程
sudo supervisorctl start all
# 检查进程状态
sudo supervisorctl status
# 停止特定进程(例如 httprelease)
sudo supervisorctl stop httprelease
# 重启特定进程
sudo supervisorctl restart httprelease
检查日志文件是否生成:
ls /home/python/tool/plat/logs
你应该看到以下文件:
httprelease.log
和 httprelease_error.log
http_up_release.log
和 http_up_release_error.log
systemctl status supervisord
。/home/python/tool/plat/logs/*_error.log
查看错误信息。/home/python/tool/plat
目录和脚本文件有正确权限,例如:sudo chmod -R 755 /home/python/tool/plat
sudo chown -R root:root /home/python/tool/plat
运行以下命令确认进程状态:
sudo supervisorctl status
输出示例:
httprelease RUNNING pid 12345, uptime 0:01:00
http_up_release RUNNING pid 12346, uptime 0:01:00
[program:httprelease]
command=python3 /home/python/tool/plat/httprelease.py
directory=/home/python/tool/plat
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=10
stdout_logfile=/home/python/tool/plat/logs/httprelease.log
stderr_logfile=/home/python/tool/plat/logs/httprelease_error.log
user=root
environment=PYTHONPATH=“/home/python/tool/plat”
[program:http_up_release]
command=python3 /home/python/tool/plat/http_up_release.py
directory=/home/python/tool/plat
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=10
stdout_logfile=/home/python/tool/plat/logs/http_up_release.log
stderr_logfile=/home/python/tool/plat/logs/http_up_release_error.log
user=root
environment=PYTHONPATH=“/home/python/tool/plat”
完成以上步骤后,Supervisor 将守护你的两个 Python 进程,并将日志输出到指定目录。