mirror of
https://github.com/wnlen/clash-for-linux.git
synced 2026-03-21 22:06:45 +08:00
Update restart.sh
This commit is contained in:
211
restart.sh
211
restart.sh
@ -1,136 +1,109 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
# 自定义action函数,实现通用action功能
|
Server_Dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
success() {
|
Service_Name="clash-for-linux.service"
|
||||||
echo -en "\\033[60G[\\033[1;32m OK \\033[0;39m]\r"
|
|
||||||
return 0
|
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 $*"; }
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
cat <<'EOF'
|
||||||
|
用法:
|
||||||
|
bash restart.sh
|
||||||
|
bash restart.sh --update
|
||||||
|
bash restart.sh --no-systemd
|
||||||
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
failure() {
|
USE_SYSTEMD="auto"
|
||||||
local rc=$?
|
DO_UPDATE="false"
|
||||||
echo -en "\\033[60G[\\033[1;31mFAILED\\033[0;39m]\r"
|
|
||||||
[ -x /bin/plymouth ] && /bin/plymouth --details
|
|
||||||
return $rc
|
|
||||||
}
|
|
||||||
|
|
||||||
action() {
|
for arg in "$@"; do
|
||||||
local STRING rc
|
case "$arg" in
|
||||||
|
--update)
|
||||||
STRING=$1
|
DO_UPDATE="true"
|
||||||
echo -n "$STRING "
|
;;
|
||||||
shift
|
--no-systemd)
|
||||||
"$@" && success $"$STRING" || failure $"$STRING"
|
USE_SYSTEMD="false"
|
||||||
rc=$?
|
;;
|
||||||
echo
|
-h|--help)
|
||||||
return $rc
|
usage
|
||||||
}
|
exit 0
|
||||||
|
;;
|
||||||
# 函数,判断命令是否正常执行
|
*)
|
||||||
if_success() {
|
err "未知参数: $arg"
|
||||||
local ReturnStatus=$3
|
usage
|
||||||
if [ $ReturnStatus -eq 0 ]; then
|
|
||||||
action "$1" /bin/true
|
|
||||||
else
|
|
||||||
action "$2" /bin/false
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
;;
|
||||||
}
|
esac
|
||||||
|
|
||||||
# 定义路劲变量
|
|
||||||
Server_Dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
|
|
||||||
Conf_Dir="$Server_Dir/conf"
|
|
||||||
Log_Dir="$Server_Dir/logs"
|
|
||||||
Temp_Dir="$Server_Dir/temp"
|
|
||||||
PID_FILE="$Temp_Dir/clash.pid"
|
|
||||||
|
|
||||||
if [ "$1" = "--update" ]; then
|
|
||||||
bash "$Server_Dir/update.sh" || exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
## 关闭clash服务
|
|
||||||
Text1="服务关闭成功!"
|
|
||||||
Text2="服务关闭失败!"
|
|
||||||
# 查询并关闭程序进程
|
|
||||||
if [ -f "$PID_FILE" ]; then
|
|
||||||
PID=$(cat "$PID_FILE")
|
|
||||||
if [ -n "$PID" ]; then
|
|
||||||
kill "$PID"
|
|
||||||
ReturnStatus=$?
|
|
||||||
for i in {1..5}; do
|
|
||||||
sleep 1
|
|
||||||
if ! kill -0 "$PID" 2>/dev/null; then
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
done
|
||||||
if kill -0 "$PID" 2>/dev/null; then
|
|
||||||
kill -9 "$PID"
|
if [ "$DO_UPDATE" = "true" ]; then
|
||||||
fi
|
if [ -f "$Server_Dir/update.sh" ]; then
|
||||||
|
info "执行更新脚本..."
|
||||||
|
bash "$Server_Dir/update.sh"
|
||||||
else
|
else
|
||||||
ReturnStatus=1
|
err "未找到 update.sh: $Server_Dir/update.sh"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
rm -f "$PID_FILE"
|
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
|
else
|
||||||
PIDS=$(pgrep -f "clash-linux-")
|
err "服务重启失败(systemd)"
|
||||||
if [ -n "$PIDS" ]; then
|
exit 1
|
||||||
kill $PIDS
|
fi
|
||||||
ReturnStatus=$?
|
}
|
||||||
for i in {1..5}; do
|
|
||||||
|
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
|
sleep 1
|
||||||
if ! pgrep -f "clash-linux-" >/dev/null; then
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
if pgrep -f "clash-linux-" >/dev/null; then
|
|
||||||
kill -9 $PIDS
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
ReturnStatus=0
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
if_success $Text1 $Text2 $ReturnStatus
|
|
||||||
|
|
||||||
sleep 3
|
if [ -f "$Server_Dir/start.sh" ]; then
|
||||||
|
bash "$Server_Dir/start.sh"
|
||||||
## 获取CPU架构
|
|
||||||
if /bin/arch &>/dev/null; then
|
|
||||||
CpuArch=`/bin/arch`
|
|
||||||
elif /usr/bin/arch &>/dev/null; then
|
|
||||||
CpuArch=`/usr/bin/arch`
|
|
||||||
elif /bin/uname -m &>/dev/null; then
|
|
||||||
CpuArch=`/bin/uname -m`
|
|
||||||
else
|
else
|
||||||
echo -e "\033[31m\n[ERROR] Failed to obtain CPU architecture!\033[0m"
|
err "未找到 start.sh: $Server_Dir/start.sh"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
## 重启启动clash服务
|
ok "服务重启成功(script)"
|
||||||
Text5="服务启动成功!"
|
}
|
||||||
Text6="服务启动失败!"
|
|
||||||
if [[ $CpuArch =~ "x86_64" ]]; then
|
if [ "$USE_SYSTEMD" = "auto" ]; then
|
||||||
nohup $Server_Dir/bin/clash-linux-amd64 -d $Conf_Dir &> $Log_Dir/clash.log &
|
if has_systemd && service_exists; then
|
||||||
PID=$!
|
USE_SYSTEMD="true"
|
||||||
ReturnStatus=$?
|
|
||||||
if [ $ReturnStatus -eq 0 ]; then
|
|
||||||
echo "$PID" > "$PID_FILE"
|
|
||||||
fi
|
|
||||||
if_success $Text5 $Text6 $ReturnStatus
|
|
||||||
elif [[ $CpuArch =~ "aarch64" || $CpuArch =~ "arm64" ]]; then
|
|
||||||
nohup $Server_Dir/bin/clash-linux-arm64 -d $Conf_Dir &> $Log_Dir/clash.log &
|
|
||||||
PID=$!
|
|
||||||
ReturnStatus=$?
|
|
||||||
if [ $ReturnStatus -eq 0 ]; then
|
|
||||||
echo "$PID" > "$PID_FILE"
|
|
||||||
fi
|
|
||||||
if_success $Text5 $Text6 $ReturnStatus
|
|
||||||
elif [[ $CpuArch =~ "armv7" ]]; then
|
|
||||||
nohup $Server_Dir/bin/clash-linux-armv7 -d $Conf_Dir &> $Log_Dir/clash.log &
|
|
||||||
PID=$!
|
|
||||||
ReturnStatus=$?
|
|
||||||
if [ $ReturnStatus -eq 0 ]; then
|
|
||||||
echo "$PID" > "$PID_FILE"
|
|
||||||
fi
|
|
||||||
if_success $Text5 $Text6 $ReturnStatus
|
|
||||||
else
|
else
|
||||||
echo -e "\033[31m\n[ERROR] Unsupported CPU Architecture!\033[0m"
|
USE_SYSTEMD="false"
|
||||||
exit 1
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$USE_SYSTEMD" = "true" ]; then
|
||||||
|
restart_by_systemd
|
||||||
|
else
|
||||||
|
restart_by_scripts
|
||||||
fi
|
fi
|
||||||
Reference in New Issue
Block a user