From 0df99ee915cbbd84c66c28a4a20c18c5fcd4f47d Mon Sep 17 00:00:00 2001 From: Mplan Date: Sat, 20 Jun 2026 12:34:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E5=AE=89=E8=A3=85=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E5=92=8C=E6=9C=8D=E5=8A=A1=E6=96=87=E4=BB=B6=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E6=88=90=E4=B8=80=E4=B8=AA=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- weather-station-receive/install-service.sh | 166 ++++++++++++++++++ .../weather-station-receiver.service | 26 --- 2 files changed, 166 insertions(+), 26 deletions(-) create mode 100755 weather-station-receive/install-service.sh delete mode 100644 weather-station-receive/weather-station-receiver.service diff --git a/weather-station-receive/install-service.sh b/weather-station-receive/install-service.sh new file mode 100755 index 0000000..fd14cb2 --- /dev/null +++ b/weather-station-receive/install-service.sh @@ -0,0 +1,166 @@ +#!/bin/bash +#=============================================================================== +# 气象站接收服务 一体化脚本 +# - 内嵌 systemd service 定义,无需单独的 .service 文件 +# - 安装 / 卸载 / 状态 / 重启 +# 用法: +# sudo ./install-service.sh # 安装 + 启用 + 启动(默认) +# sudo ./install-service.sh install # 同上 +# sudo ./install-service.sh uninstall # 停止 + 禁用 + 删除 service +# sudo ./install-service.sh status # 查看运行状态 +# sudo ./install-service.sh restart # 重启服务 +#=============================================================================== +set -uo pipefail + +SERVICE_NAME="weather-station-receiver" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +TARGET_SCRIPT="${SCRIPT_DIR}/receive.sh" +DST_SERVICE="/etc/systemd/system/${SERVICE_NAME}.service" + +ACTION="${1:-install}" + +#------------------------------------------------------------------------------- +# 必须以 root 运行 +#------------------------------------------------------------------------------- +check_root() { + if (( EUID != 0 )); then + echo "[错误] 请使用 root 运行: sudo $0 $ACTION" + exit 1 + fi +} + +#------------------------------------------------------------------------------- +# 内嵌的 systemd service 单元文件 +# WorkingDirectory / ExecStart 使用脚本所在目录,移动目录后重新安装即可 +#------------------------------------------------------------------------------- +write_service_file() { + cat > "$DST_SERVICE" < daemon-reload -> enable -> start +#------------------------------------------------------------------------------- +do_install() { + echo "========================================" + echo " 安装气象站接收服务" + echo "========================================" + preflight + + echo "[1/5] 生成 service 文件 -> $DST_SERVICE" + write_service_file + + echo "[2/5] 重新加载 systemd" + systemctl daemon-reload + + echo "[3/5] 启用开机自启" + systemctl enable "$SERVICE_NAME" + + echo "[4/5] 启动服务" + systemctl restart "$SERVICE_NAME" + + echo "[5/5] 运行状态:" + systemctl --no-pager --full status "$SERVICE_NAME" || true + echo "========================================" + echo " 安装完成" + echo " 查看日志: journalctl -u $SERVICE_NAME -f" + echo "========================================" +} + +#------------------------------------------------------------------------------- +# 卸载:stop -> disable -> remove -> daemon-reload +#------------------------------------------------------------------------------- +do_uninstall() { + echo "========================================" + echo " 卸载气象站接收服务" + echo "========================================" + echo "[1/4] 停止服务" + systemctl stop "$SERVICE_NAME" 2>/dev/null || true + + echo "[2/4] 禁用开机自启" + systemctl disable "$SERVICE_NAME" 2>/dev/null || true + + echo "[3/4] 删除 service 文件" + rm -f "$DST_SERVICE" + + echo "[4/4] 重新加载 systemd" + systemctl daemon-reload + systemctl reset-failed "$SERVICE_NAME" 2>/dev/null || true + echo "========================================" + echo " 卸载完成" + echo "========================================" +} + +#------------------------------------------------------------------------------- +# 状态 +#------------------------------------------------------------------------------- +do_status() { + systemctl --no-pager --full status "$SERVICE_NAME" || true + echo + echo "开机自启: $(systemctl is-enabled "$SERVICE_NAME" 2>/dev/null || echo '未知')" + echo "当前状态: $(systemctl is-active "$SERVICE_NAME" 2>/dev/null || echo '未知')" +} + +#------------------------------------------------------------------------------- +# 重启 +#------------------------------------------------------------------------------- +do_restart() { + systemctl restart "$SERVICE_NAME" + do_status +} + +#------------------------------------------------------------------------------- +# 主入口 +#------------------------------------------------------------------------------- +check_root +case "$ACTION" in + install) do_install ;; + uninstall) do_uninstall ;; + status) do_status ;; + restart) do_restart ;; + *) + echo "用法: sudo $0 {install|uninstall|status|restart}" + exit 1 + ;; +esac diff --git a/weather-station-receive/weather-station-receiver.service b/weather-station-receive/weather-station-receiver.service deleted file mode 100644 index ce8c82a..0000000 --- a/weather-station-receive/weather-station-receiver.service +++ /dev/null @@ -1,26 +0,0 @@ -[Unit] -Description=Weather Station Serial Receiver -After=network-online.target -Wants=network-online.target - -[Service] -Type=simple -User=root -WorkingDirectory=/home/arch/script/weather-station-receive -ExecStart=/bin/bash /home/arch/script/weather-station-receive/receive.sh - -# 自动重启 -Restart=always -RestartSec=10 - -# 防止无限重启:60 秒内超过 3 次则停止 -StartLimitInterval=60 -StartLimitBurst=3 - -# 日志输出到 journald -StandardOutput=journal -StandardError=journal -SyslogIdentifier=weather-station - -[Install] -WantedBy=multi-user.target