-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeep-oracle.sh
More file actions
171 lines (148 loc) · 4.63 KB
/
keep-oracle.sh
File metadata and controls
171 lines (148 loc) · 4.63 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# Author: An Shen
# Date: 2023-01-30
. /etc/profile
function log(){
echo "[$(date +'%Y-%m-%d %H:%M:%S')] - $1"
}
function get_latest_info(){
local latest_info_file='/tmp/NeverIdle-latest-info.json'
wget -q -O ${latest_info_file} https://api.github.com/repos/layou233/NeverIdle/releases/latest
[[ $? -ne 0 ]] && log "Failed to get latest info" && exit 1
latest_version=$(grep tag_name ${latest_info_file} | cut -d '"' -f 4| sed 's/^v//g')
latest_comments=$(grep body ${latest_info_file} | cut -d '"' -f 4)
rm -f ${latest_info_file}
}
function auto_set_mem_test_size(){
mem_test='-m 2'
if [[ $mem_total -lt 4 ]]
then
log "AMD doesn't need to test memory !"
mem_test=''
elif [[ $mem_total -lt 13 ]]
then
log "Memory test size: [1G]"
mem_test='-m 1'
else
log "Memory test size: [4G]"
fi
}
function download_and_run() {
local base_download_url="https://github.com/layou233/NeverIdle/releases/download"
local filename="NeverIdle-${platform}"
local download_dir="/tmp"
local download_url="${base_download_url}/${latest_version}/${filename}"
mkdir -p $download_dir
rm -f ${download_dir}/NeverIdle
log "Downloading ${filename} to ${download_dir}/NeverIdle ..."
wget -q -O ${download_dir}/NeverIdle ${download_url}
[[ $? -ne 0 ]] && log "Download ${filename} failed" && exit 1
chmod +x ${download_dir}/NeverIdle
if [[ ${memory_test_size} -gt $mem_total ]] || [[ "x${memory_test_size}" == "x" ]]
then
log "invalid memory size: [${memory_test_size}]"
auto_set_mem_test_size
elif [[ "x${memory_test_size}" == "x0" ]]
then
mem_test=''
log "Memory test disabled."
else
mem_test="-m ${memory_test_size}"
log "Memory test size: [${memory_test_size}]"
fi
local cpu_test="-c 1H"
if [[ "x${cpu_test_interval}" == "x" ]]
then
local cpu_test="-c 1H"
log "cpu test interval is empty, set to default value: [1H]"
elif [[ "x${cpu_test_interval}" == "x0" ]]
then
local cpu_test="-c 1H"
log "cpu test can't disable, set to default value: [1H]."
else
local cpu_test="-c ${cpu_test_interval}h"
log "cpu test interval: [${cpu_test_interval}h]"
fi
local network_test="-n 2H"
if [[ "x${network_test_interval}" == "x" ]]
then
local network_test="-n 2H"
log "network test interval is empty, set to default value: [2H]"
elif [[ "x${network_test_interval}" == "x0" ]]
then
local network_test=""
log "network test diabled."
else
local network_test="-n ${network_test_interval}h"
log "network test interval: ${network_test_interval}"
fi
log "cmd: ${download_dir}/NeverIdle ${cpu_test} ${mem_test} ${network_test}"
nohup ${download_dir}/NeverIdle ${cpu_test} ${mem_test} ${network_test} > ${download_dir}/NeverIdle.log 2>&1 &
local pid=$(pgrep NeverIdle)
log "NeverIdle [${pid}] is running"
log "run 'pkill NeverIdle' to stop it."
log "log file: ${download_dir}/NeverIdle.log"
}
function print_help_msg(){
echo "usage:"
echo -e "\t-c \t cpu test interval, defaut: 1H, can't disable."
echo -e "\t-m \t memory test size, value/total memory range: 0/<4 1G/<13 4G/>13, 0 to disable test"
echo -e "\t-n \t network test interval, 0 to disable test"
echo -e "\t-h \t show help info."
exit 0
}
function read_args(){
while getopts ":c:m:n:h" opt; do
case "$opt" in
c)
cpu_test_interval="$OPTARG";;
n)
network_test_interval="$OPTARG";;
m)
memory_test_size="$OPTARG";;
h)
print_help_msg;;
esac
done
if [[ "${cpu_test_interval}" -lt 0 ]]
then
log "invalid cpu test interval [${cpu_test_interval}], set to 2"
cpu_test_interval="2"
fi
if [[ "${network_test_interval}" -lt 0 ]]
then
log "invalid network test interval [${network_test_interval}], set to 4"
network_test_interval="4"
fi
if [[ "${memory_test_size}" -lt 0 ]]
then
log "invalid memory size [${memory_test_size}]"
memory_test_size=""
fi
}
function init(){
mem_total=$(free -g | awk '/Mem/ {print $2}')
case $(uname -m) in
"x86_64")
platform="linux-amd64"
;;
"aarch64")
platform="linux-arm64"
;;
*)
log "Unsupported platform !"
exit 1
;;
esac
}
function keep_stop(){
pkill -9 NeverIdle
}
function main(){
keep_stop
init
get_latest_info
download_and_run
}
read_args "$@"
main