一、准备信息(必填 5 项)
| 变量 | 示例值 | 含义 |
|---|---|---|
PROJ_DIR | /www/wwwroot/newProject | 项目根目录 |
PORT | 8080 | 项目监听的本地端口 |
USER | newuser | 运行项目的 Linux 用户 |
TITLE | egg-server-NewProject | egg-scripts 的 --title 参数(或其他框架启动参数) |
SERVICE | newproject | systemd 服务名(自定义,全小写无空格) |
二、生成并启用 systemd 单元(一次复制即可)
1. 主服务 /etc/systemd/system/${SERVICE}.service
[Unit]
Description=${SERVICE} on port ${PORT}
After=network.target
[Service]
Type=simple
User=${USER}
WorkingDirectory=${PROJ_DIR}
Environment="PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin"
Environment="HOME=/home/${USER}"
# ① Node/Egg 通用前台启动(无 --daemon)
ExecStart=/usr/bin/node ${PROJ_DIR}/node_modules/.bin/egg-scripts start --title=${TITLE}
# ② 其他框架示例(按需替换)
# ExecStart=/usr/bin/node ${PROJ_DIR}/app.js
# ExecStart=/usr/bin/npm run start:front
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target

2. 看门狗脚本 /opt/${SERVICE}-watchdog.sh
#!/usr/bin/env bash
PORT=${PORT} # 改这里
SERVICE=${SERVICE} # 改这里
ss -lnt | grep -q ":${PORT} " && exit 0
systemctl start "$SERVICE"
sudo chmod +x /opt/${SERVICE}-watchdog.sh

3. 看门狗服务 /etc/systemd/system/${SERVICE}-watchdog.service
[Unit]
Description=Watchdog for ${SERVICE} port ${PORT}
[Service]
Type=oneshot
ExecStart=/opt/${SERVICE}-watchdog.sh

4. 看门狗定时器 /etc/systemd/system/${SERVICE}-watchdog.timer
[Unit]
Description=Check port ${PORT} every 30s
Requires=${SERVICE}.service
[Timer]
OnBootSec=30s
OnUnitActiveSec=30s # 5 分钟则改成 300s
Unit=${SERVICE}-watchdog.service
[Install]
WantedBy=timers.target

三、通用一键替换模板(复制即用)
# ====== 仅需修改下面 5 行 ======
PROJ_DIR=/www/wwwroot/newProject
PORT=8080
USER=newuser
TITLE=egg-server-NewProject
SERVICE=newproject
# ==============================
# 生成并启用
sudo tee /etc/systemd/system/${SERVICE}.service >/dev/null <<EOF
[Unit]
Description=${SERVICE} on port ${PORT}
After=network.target
[Service]
Type=simple
User=${USER}
WorkingDirectory=${PROJ_DIR}
Environment="PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin"
Environment="HOME=/home/${USER}"
ExecStart=/usr/bin/node ${PROJ_DIR}/node_modules/.bin/egg-scripts start --title=${TITLE}
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
EOF
sudo tee /opt/${SERVICE}-watchdog.sh >/dev/null <<EOF
#!/usr/bin/env bash
PORT=${PORT}
SERVICE=${SERVICE}
ss -lnt | grep -q ":${PORT} " && exit 0
systemctl start "\$SERVICE"
EOF
sudo chmod +x /opt/${SERVICE}-watchdog.sh
sudo tee /etc/systemd/system/${SERVICE}-watchdog.service >/dev/null <<EOF
[Unit]
Description=Watchdog for ${SERVICE} port ${PORT}
[Service]
Type=oneshot
ExecStart=/opt/${SERVICE}-watchdog.sh
EOF
sudo tee /etc/systemd/system/${SERVICE}-watchdog.timer >/dev/null <<EOF
[Unit]
Description=Check port ${PORT} every 30s
Requires=${SERVICE}.service
[Timer]
OnBootSec=30s
OnUnitActiveSec=30s
Unit=${SERVICE}-watchdog.service
[Install]
WantedBy=timers.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now ${SERVICE}.service ${SERVICE}-watchdog.timer
四、通用验证步骤(任何项目都照做)
1.看状态
sudo systemctl status ${SERVICE}.service
sudo systemctl status ${SERVICE}-watchdog.timer
ss -lnt | grep ${PORT}
2.制造掉线
sudo pkill -f ${TITLE} # 或 lsof -ti:${PORT} | xargs sudo kill -9
3.等待30s
watch -n 1 'ss -lnt | grep ${PORT}'
端口重新出现 → 成功。
4.看日志
sudo journalctl -u ${SERVICE} -e
sudo journalctl -u ${SERVICE}-watchdog -e
五、日常管理命令(通用)
| 操作 | 命令 |
|---|---|
| 重启项目 | sudo systemctl restart ${SERVICE} |
| 停止项目 | sudo systemctl stop ${SERVICE} |
| 实时日志 | sudo journalctl -u ${SERVICE} -f |
| 关闭自动检测 | sudo systemctl disable --now ${SERVICE}-watchdog.timer |
| 彻底卸载 | sudo systemctl disable --now ${SERVICE} ${SERVICE}-watchdog.timer && sudo rm /etc/systemd/system/${SERVICE}.* /opt/${SERVICE}-watchdog.sh |

423

被折叠的 条评论
为什么被折叠?



