网站首页 > 技术文章 正文
以下是将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
注意事项:
- 确保脚本中所有文件路径都是绝对路径
- 脚本中不要使用input()等交互式函数
- 数据库连接等配置最好使用环境变量或配置文件
- 建议添加异常处理确保服务不会意外退出
- 对于生产环境,考虑添加监控和告警机制
这样设置后,你的脚本将作为系统服务在后台持续运行,即使服务器重启也会自动启动。
如果在 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 环境是否与运行服务的环境一致。
猜你喜欢
- 2025-05-11 Python包导入指南:从菜鸟到专家的import魔法
- 2025-05-11 一文学会用PyInstaller把Python 程序打包成一个独立的可执行文件
- 2025-05-11 如何在python中安装和配置kivy库
- 2025-05-11 深入了解Python的setup.py文件
- 2025-05-11 Python程序员必看!10分钟搞定项目打包:PyInstaller保姆级教程
- 2025-05-11 Python程序开发入门(第六章)函数与模块
- 2025-05-11 Python保姆级安装教程(CPU+GPU)
- 2025-05-11 Python 中的函数和模块
- 2025-05-11 python 进阶突破——内置模块(Standard Library)
- 2025-05-11 Python中h5py与netCDF4模块在Anaconda环境的下载与安装
- 05-27程序员用 Python 爬取抖音高颜值美女
- 05-27YOLO v3、FaceNet和SVM的人脸检测识别系统源码(python)分享
- 05-27「工具推荐」世界上最简单的人脸识别库 44.7 star
- 05-27开源人脸识别系统源码推荐
- 05-27Go 人脸识别教程
- 05-27Python 深度学习之人脸识别(yolo+facenet)
- 05-27简单的Py人脸识别
- 05-27Python编程 - 基于OpenCV实现人脸识别(实践篇)爬虫+人脸识别
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 91℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 82℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)