mirror of
http://junepve.top:3000/chicrobot/README.git
synced 2026-10-29 22:51:29 +08:00
57 lines
1021 B
Markdown
57 lines
1021 B
Markdown
## 创建服务文件
|
||
以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
|
||
``` |