服务器部署爬虫:Supervisor 进程守护

在服务器长期运行爬虫时,常会遇到程序意外崩溃、断连退出、后台挂起失效等问题,直接用 nohup 或 screen 管理不够规范、不够稳定。Supervisor 是 Linux 下轻量、可靠的进程守护工具,专门用来托管爬虫、服务、脚本等后台程序,实现自动重启、日志管理、统一管控。

一、Supervisor 是什么

Supervisor 是用 Python 开发的进程管理系统,核心作用:

  • 让程序在后台稳定运行
  • 程序崩溃自动重启
  • 集中管理启动、停止、重启、查看状态
  • 统一管理标准输出与错误日志非常适合爬虫、API 服务、定时任务等长期驻留进程。

二、安装 Supervisor

以 CentOS / Ubuntu 为例:

CentOS/RHEL

bash

运行

复制代码
yum install -y epel-release
yum install -y supervisor

Ubuntu/Debian

bash

运行

复制代码
apt update
apt install -y supervisor

安装完成后,启动并设置开机自启:

bash

运行

复制代码
systemctl start supervisord
systemctl enable supervisord

检查是否运行:

bash

运行

复制代码
ps aux | grep supervisord

三、编写爬虫脚本(示例)

假设你的爬虫文件:/home/spider/main.py

python

运行

复制代码
import time
import datetime

while True:
    print(f"[{datetime.datetime.now()}] 爬虫正在运行...")
    # 这里写你的爬取逻辑
    time.sleep(10)

确保爬虫能直接在命令行正常运行

bash

运行

复制代码
python3 /home/spider/main.py

四、配置 Supervisor 托管爬虫

Supervisor 配置目录通常为:

  • /etc/supervisord.d/

新建配置文件,建议以 .ini 结尾:

bash

运行

复制代码
vim /etc/supervisord.d/spider.ini

写入如下配置(可直接复制修改路径使用):

ini

复制代码
[program:spider]
command=python3 /home/spider/main.py
directory=/home/spider
user=root
autostart=true
autorestart=true
startretries=3
stdout_logfile=/var/log/spider.log
stderr_logfile=/var/log/spider_err.log

配置说明

  • [program:spider]:进程名称,自定义
  • command:启动命令
  • directory:工作目录
  • autostart=true:服务器开机自动启动
  • autorestart=true:程序崩溃自动重启
  • stdout_logfile:正常日志
  • stderr_logfile:错误日志

五、更新配置并管理爬虫

  1. 重新加载配置

bash

运行

复制代码
supervisorctl reread
supervisorctl update
  1. 启动爬虫

bash

运行

复制代码
supervisorctl start spider
  1. 查看状态

bash

运行

复制代码
supervisorctl status
  1. 常用命令

bash

运行

复制代码
supervisorctl stop spider      # 停止
supervisorctl restart spider   # 重启
supervisorctl reload           # 重载所有配置

六、查看日志(排查爬虫问题)

实时查看爬虫日志:

bash

运行

复制代码
tail -f /var/log/spider.log

查看错误日志:

bash

运行

复制代码
tail -f /var/log/spider_err.log

日志能帮你快速定位:

  • 爬虫崩溃原因
  • 网络超时
  • 解析错误
  • 权限问题

七、为什么推荐 Supervisor 托管爬虫

  • 稳定:崩溃自动重启,避免爬一半中断
  • 统一管理:一台服务器可管理 N 个爬虫,互不干扰
  • 日志规范:方便排查线上问题
  • 轻量无依赖:比 systemd 更简单、比 screen 更专业
  • 适合长期运行:爬虫 7×24 小时稳定在线

八、常见问题

  1. 报错:spider: ERROR (spawn error)

    • 检查 Python 路径是否正确
    • 检查日志文件权限
    • 手动执行 command 命令看是否能运行
  2. 程序一直重启

    • 代码有异常,看 stderr 日志
    • 爬虫执行完就退出,不是死循环程序不适合托管
  3. 修改代码后不生效

    • 必须 restart 进程:

    bash

    运行

    复制代码
    supervisorctl restart spider

总结

服务器部署爬虫,Supervisor 是标配进程守护方案。只要三步:安装 → 写配置 → 启动托管,就能让爬虫稳定、安全、自动运行,彻底解决后台掉线、崩溃、无日志等问题。

相关推荐
毕胜客源码1 小时前
卷积神经网络的农作物识别系统(有技术文档)深度学习 图像识别 卷积神经网络 Django python 人工智能
人工智能·python·深度学习·cnn·django
a1117761 小时前
jetpack5.0对应版本的torch和torchvision
python·开源·torch
Where-1 小时前
LangChain核心组件-Tool
python·langchain
angushine2 小时前
Python常用方法
开发语言·前端·python
【 】4232 小时前
pyhon相对导入
开发语言·python
西门大盗2 小时前
pycharm自动进行python 测试(python test)
ide·python·pycharm
Jmayday2 小时前
Pytorch:张量的操作
人工智能·pytorch·python
石榴树下的七彩鱼2 小时前
智能抠图 API 多语言接入实战:从零到上线的 Python / Java / PHP / JS 完整教程(附避坑指南)
java·python·php·智能抠图·api接入·石榴智能·shiliuai
Captain_Data3 小时前
AI 12小时设计CPU完整解析:从219字到RISC-V内核的技术突破
人工智能·python·ai·大模型·芯片设计·risc-v
小鱼~~3 小时前
最小二乘&均方误差MSE&平均绝对误差MAE
python·算法·机器学习