添加 e1_linux添加systemd服务.md

This commit is contained in:
chicrobot committed 2026-01-21 16:18:36 +08:00
1 parent 13f71faeca
commit 6beba4c147
1 file changed
+57
+57
View File
@@ -0,0 +1,57 @@
## 创建服务文件
以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
```