#!/bin/bash
current_time() { date "+[%Y-%m-%d %H:%M:%S]" }
DOWNLOAD_DIR="/opt/program" PROGRAM_DIR="$DOWNLOAD_DIR/AdGuardHome"
[ ! -d "$DOWNLOAD_DIR" ] && mkdir -p "$DOWNLOAD_DIR" [ ! -d "$PROGRAM_DIR" ] && mkdir -p "$PROGRAM_DIR"
echo "$(current_time) 下载最新版 AdguardHome..." wget -q -O "$DOWNLOAD_DIR/AdGuardHome_linux_amd64.tar.gz" https://static.adtidy.org/adguardhome/release/AdGuardHome_linux_amd64.tar.gz & wget_pid=$!
timeout=180 elapsed_time=0 while ps -p $wget_pid > /dev/null; do sleep 1 elapsed_time=$((elapsed_time + 1)) if [ $elapsed_time -ge $timeout ]; then echo "$(current_time) 下载超时。终止进程。" kill $wget_pid rm -rf "$DOWNLOAD_DIR/AdGuardHome_linux_amd64.tar.gz" exit 1 fi done
if wait $wget_pid; then echo "$(current_time) 下载已完成!" else echo "$(current_time) 下载失败。" rm -rf "$DOWNLOAD_DIR/AdGuardHome_linux_amd64.tar.gz" exit 1 fi
echo "$(current_time) 解压安装包 AdguardHome..." if tar -zxvf "$DOWNLOAD_DIR/AdGuardHome_linux_amd64.tar.gz" -C "$DOWNLOAD_DIR" > /dev/null; then rm -rf "$DOWNLOAD_DIR/AdGuardHome_linux_amd64.tar.gz" else echo "$(current_time) 解压失败。" rm -rf "$DOWNLOAD_DIR/AdGuardHome_linux_amd64.tar.gz" exit 1 fi
if pkill AdGuardHome; then echo "$(current_time) 停止现有的 AdGuardHome 进程。" else echo "$(current_time) 没有正在运行的 AdGuardHome 进程。" fi
if rm -rf /etc/systemd/system/AdGuardHome.service; then echo "$(current_time) 删除旧的 AdGuardHome 服务文件。" else echo "$(current_time) 删除旧的 AdGuardHome 服务文件失败。" exit 1 fi
echo "$(current_time) 安装 AdguardHome..." "$PROGRAM_DIR/AdGuardHome" -s install
if systemctl is-active --quiet AdGuardHome; then echo "$(current_time) AdGuardHome 安装成功!" else echo "$(current_time) AdGuardHome 服务启动失败。" exit 1 fi
echo "$(current_time) 脚本执行完毕。"
|