程序员文章、书籍推荐和程序员创业信息与资源分享平台

网站首页 > 技术文章 正文

ubuntu部署python脚本为系统服务

hfteth 2025-05-11 15:49:38 技术文章 8 ℃

以下是将Python脚本设置为Ubuntu系统服务的完整步骤:

1. 创建系统服务文件

创建一个新的systemd服务单元文件:

sudo nano /etc/systemd/system/wechat_msg_service.service

文件内容如下(根据你的实际路径修改):

[Unit]
Description=WeChat Message Notification Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/path/to/your/script/directory
ExecStart=/usr/bin/python3 /path/to/your/script/wechat_msg_service.py
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=wechat_msg_service
Environment=PYTHONUNBUFFERED=1

[Install]
WantedBy=multi-user.target

2. 设置脚本权限

sudo chmod 644 /etc/systemd/system/wechat_msg_service.service
sudo chmod +x /path/to/your/script/wechat_msg_service.py

3. 重新加载systemd配置

sudo systemctl daemon-reload

4. 启动服务并设置开机自启

sudo systemctl start wechat_msg_service
sudo systemctl enable wechat_msg_service

5. 检查服务状态

sudo systemctl status wechat_msg_service

6. 查看日志

journalctl -u wechat_msg_service -f

7. 常用管理命令

# 停止服务
sudo systemctl stop wechat_msg_service

# 重启服务
sudo systemctl restart wechat_msg_service

# 禁用开机自启
sudo systemctl disable wechat_msg_service

8. 确保Python环境正确

如果你的脚本使用虚拟环境,修改服务文件中的ExecStart:

ExecStart=/path/to/venv/bin/python /path/to/your/script/wechat_msg_service.py

9. 添加日志轮转配置(可选)

创建日志轮转配置:

sudo nano /etc/logrotate.d/wechat_msg_service

内容如下:

/var/log/syslog
{
    rotate 7
    daily
    missingok
    notifempty
    delaycompress
    compress
    postrotate
        /usr/bin/systemctl kill -s HUP rsyslog.service >/dev/null 2>&1 || true
    endscript
}

10. 确保所有依赖已安装

sudo apt update
sudo apt install python3-pip
pip3 install pymysql requests schedule

注意事项:

  1. 确保脚本中所有文件路径都是绝对路径
  2. 脚本中不要使用input()等交互式函数
  3. 数据库连接等配置最好使用环境变量或配置文件
  4. 建议添加异常处理确保服务不会意外退出
  5. 对于生产环境,考虑添加监控和告警机制

这样设置后,你的脚本将作为系统服务在后台持续运行,即使服务器重启也会自动启动。

如果在 Ubuntu 上运行服务时遇到 No module named 'schedule' 错误,说明 Python 环境中缺少 schedule 模块。以下是完整的解决方案:

1. 安装缺少的 Python 模块

# 对于系统全局安装(推荐方式)
sudo pip3 install schedule pymysql requests

# 或者如果使用虚拟环境
/path/to/your/venv/bin/pip install schedule pymysql requests

2. 确认 Python 路径

检查服务文件中使用的 Python 路径是否正确:

which python3

然后更新服务文件中的 ExecStart 行:

ExecStart=/usr/bin/python3 /path/to/your/script/wechat_msg_service.py

3. 如果使用虚拟环境

修改服务文件为:

ExecStart=/path/to/venv/bin/python /path/to/your/script/wechat_msg_service.py

4. 验证安装

手动测试模块是否可用:

python3 -c "import schedule; import pymysql; import requests; print('All modules imported successfully')"

5. 重新加载并重启服务

sudo systemctl daemon-reload
sudo systemctl restart wechat_msg_service

6. 检查错误日志

journalctl -u wechat_msg_service -f --no-pager

7. 备选方案:使用系统包管理器

如果 pip 安装不成功,可以尝试:

# 对于 Ubuntu/Debian
sudo apt-get install python3-schedule  # 注意:可能不是最新版

# 或者确保 pip 是最新版
sudo apt-get install python3-pip
sudo pip3 install --upgrade pip

8. 永久解决依赖问题

创建 requirements.txt 文件:

echo "schedule==1.1.0
pymysql==1.0.2
requests==2.28.1" > /path/to/your/script/requirements.txt

然后安装:

sudo pip3 install -r /path/to/your/script/requirements.txt

9. 检查 Python 版本兼容性

确保你的脚本使用的 Python 版本与安装模块的版本一致:

python3 --version
pip3 --version

10. 最终验证

# 检查已安装模块
pip3 list | grep -E "schedule|pymysql|requests"

# 测试脚本直接运行
python3 /path/to/your/script/wechat_msg_service.py

完成以上步骤后,服务应该能正常启动并找到所有需要的 Python 模块。如果问题仍然存在,请检查 Python 环境是否与运行服务的环境一致。

最近发表
标签列表