forked from LopSdir/clash-for-linux
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathrestart.sh
executable file
·112 lines (95 loc) · 2.21 KB
/
restart.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
# 定义颜色和样式
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# 自定义action函数,实现通用action功能
success() {
echo -e "${GREEN}[ OK ]${NC}"
return 0
}
failure() {
local rc=$?
echo -e "${RED}[FAILED]${NC}"
[ -x /bin/plymouth ] && /bin/plymouth --details
return $rc
}
action() {
local STRING=$1
echo -n "$STRING "
shift
"$@" && success || failure
local rc=$?
echo
return $rc
}
# 函数,判断命令是否正常执行
if_success() {
local message_success=$1
local message_failure=$2
local return_status=${3:-0} # 如果 \$3 未设置或为空,则默认为 0
if [ "$return_status" -eq 0 ]; then
action "$message_success" /bin/true
else
action "$message_failure" /bin/false
exit 1
fi
}
# 定义路径变量
Server_Dir="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
Conf_Dir="$Server_Dir/conf"
Log_Dir="$Server_Dir/logs"
# 关闭clash服务
close_clash_service() {
local pid_num=$(pgrep -c clash-linux)
local pid=$(pgrep clash-linux)
local return_status=0
if [ "$pid_num" -ne 0 ]; then
kill "$pid" &>/dev/null
return_status=$?
fi
if_success "服务关闭成功!" "服务关闭失败!" "$return_status"
}
# 获取CPU架构
get_cpu_arch() {
if /bin/arch &>/dev/null; then
echo $(/bin/arch)
elif /usr/bin/arch &>/dev/null; then
echo $(/usr/bin/arch)
elif /bin/uname -m &>/dev/null; then
echo $(/bin/uname -m)
else
echo -e "${RED}[ERROR] Failed to obtain CPU architecture!${NC}"
exit 1
fi
}
# 启动clash服务
start_clash_service() {
local cpu_arch=$(get_cpu_arch)
local clash_binary
case $cpu_arch in
x86_64)
clash_binary="clash-linux-amd64"
;;
aarch64|arm64)
clash_binary="clash-linux-arm64"
;;
armv7)
clash_binary="clash-linux-armv7"
;;
*)
echo -e "${RED}[ERROR] Unsupported CPU Architecture!${NC}"
exit 1
;;
esac
nohup "$Server_Dir/bin/$clash_binary" -d "$Conf_Dir" &> "$Log_Dir/clash.log" &
local return_status=$?
if_success "服务启动成功!" "服务启动失败!" "$return_status"
}
# 主程序
main() {
close_clash_service
sleep 3
start_clash_service
}
main