Files
README/e1_linux添加systemd服务.md
T

57 lines
1021 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## 创建服务文件
以pyhon项目为例
编辑
```
sudo nano /etc/systemd/system/myblog.service
```
文件内容
```
[Unit]
Description=My Blog Application
After=network.target
[Service]
Type=simple
User=your_username # ← 替换为你的用户名(如 ubuntu、root 等)
WorkingDirectory=/path/to/your/project # ← 项目根目录(包含 .venv 和 main.py)
ExecStart=/path/to/your/project/.venv/bin/python main.py
Restart=always
RestartSec=10
# 可选:设置环境变量
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
```
## 重载systemd服务并启用服务
```
# 重载配置
sudo systemctl daemon-reload
# 开机自启(可选)
sudo systemctl enable myblog.service
# 启动服务
sudo systemctl start myblog.service
```
## 相关命令
```
# 启动
sudo systemctl start myblog
# 停止
sudo systemctl stop myblog
# 重启
sudo systemctl restart myblog
# 查看状态
sudo systemctl status myblog
# 查看日志(实时)
sudo journalctl -u myblog -f
```