From 6beba4c14778bfd81d8aff027f234f192ae347f8 Mon Sep 17 00:00:00 2001 From: chicrobot Date: Wed, 21 Jan 2026 16:18:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20e1=5Flinux=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0systemd=E6=9C=8D=E5=8A=A1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- e1_linux添加systemd服务.md | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 e1_linux添加systemd服务.md diff --git a/e1_linux添加systemd服务.md b/e1_linux添加systemd服务.md new file mode 100644 index 0000000..d2a4f04 --- /dev/null +++ b/e1_linux添加systemd服务.md @@ -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 +``` \ No newline at end of file