Update restart.sh

This commit is contained in:
wnlen
2026-03-20 15:32:21 +08:00
parent 8f5b2c66b2
commit ee0c44302f

View File

@ -1,109 +1,15 @@
#!/usr/bin/env bash
set -euo pipefail
set -e
Server_Dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
Service_Name="clash-for-linux.service"
SERVICE="clash-for-linux.service"
log() { printf "%b\n" "$*"; }
info() { log "\033[36m[INFO]\033[0m $*"; }
ok() { log "\033[32m[OK]\033[0m $*"; }
warn() { log "\033[33m[WARN]\033[0m $*"; }
err() { log "\033[31m[ERROR]\033[0m $*"; }
echo "[INFO] Updating config..."
usage() {
cat <<'EOF'
用法:
bash restart.sh
bash restart.sh --update
bash restart.sh --no-systemd
EOF
}
# 只负责生成配置,不启动内核
bash start.sh --only-generate
USE_SYSTEMD="auto"
DO_UPDATE="false"
echo "[INFO] Restarting systemd service..."
for arg in "$@"; do
case "$arg" in
--update)
DO_UPDATE="true"
;;
--no-systemd)
USE_SYSTEMD="false"
;;
-h|--help)
usage
exit 0
;;
*)
err "未知参数: $arg"
usage
exit 1
;;
esac
done
systemctl restart "$SERVICE"
if [ "$DO_UPDATE" = "true" ]; then
if [ -f "$Server_Dir/update.sh" ]; then
info "执行更新脚本..."
bash "$Server_Dir/update.sh"
else
err "未找到 update.sh: $Server_Dir/update.sh"
exit 1
fi
fi
has_systemd() {
command -v systemctl >/dev/null 2>&1
}
service_exists() {
systemctl list-unit-files 2>/dev/null | grep -q "^clash-for-linux.service"
}
restart_by_systemd() {
info "使用 systemd 重启 Clash 服务..."
systemctl restart "$Service_Name"
systemctl --no-pager --full status "$Service_Name" || true
if systemctl is-active --quiet "$Service_Name"; then
ok "服务重启成功systemd"
else
err "服务重启失败systemd"
exit 1
fi
}
restart_by_scripts() {
info "使用脚本方式重启 Clash..."
if [ -f "$Server_Dir/shutdown.sh" ]; then
bash "$Server_Dir/shutdown.sh" || true
else
warn "未找到 shutdown.sh跳过关闭步骤"
fi
sleep 1
if [ -f "$Server_Dir/start.sh" ]; then
bash "$Server_Dir/start.sh"
else
err "未找到 start.sh: $Server_Dir/start.sh"
exit 1
fi
ok "服务重启成功script"
}
if [ "$USE_SYSTEMD" = "auto" ]; then
if has_systemd && service_exists; then
USE_SYSTEMD="true"
else
USE_SYSTEMD="false"
fi
fi
if [ "$USE_SYSTEMD" = "true" ]; then
restart_by_systemd
else
restart_by_scripts
fi
echo "[OK] Clash restarted via systemd"