通用“Node 项目端口守护 + 自动重启”systemd 方案

一、准备信息(必填 5 项)

变量示例值含义
PROJ_DIR/www/wwwroot/newProject项目根目录
PORT8080项目监听的本地端口
USERnewuser运行项目的 Linux 用户
TITLEegg-server-NewProjectegg-scripts 的 --title 参数(或其他框架启动参数)
SERVICEnewprojectsystemd 服务名(自定义,全小写无空格)

二、生成并启用 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值