Skip to content

Instantly share code, notes, and snippets.

@Ansen
Last active January 17, 2024 01:09
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Ansen/e45320205faf5786d3282ac880f20bab to your computer and use it in GitHub Desktop.
Save Ansen/e45320205faf5786d3282ac880f20bab to your computer and use it in GitHub Desktop.
#!/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: [2G]"
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 2h"
if [[ "x${cpu_test_interval}" == "x" ]]
then
local cpu_test="-c 2h"
log "cpu test interval is empty, set to default value: [2h]"
elif [[ "x${cpu_test_interval}" == "x0" ]]
then
local cpu_test="-c 2h"
log "cpu test can't disable, set to default value: [2h]."
else
local cpu_test="-c ${cpu_test_interval}h"
log "cpu test interval: [${cpu_test_interval}h]"
fi
local network_test="-n 4h"
if [[ "x${network_test_interval}" == "x" ]]
then
local network_test="-n 4h"
log "network test interval is empty, set to default value: [4h]"
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 "log file: ${download_dir}/NeverIdle.log"
log "========================================"
log "run 'pkill NeverIdle' to stop it."
log "run 'rm -f ${download_dir}/NeverIdle ${download_dir}/NeverIdle.log' to clean it."
}
function print_help_msg(){
echo "usage:"
echo -e "\t-c \t cpu test interval, defaut: 2h, can't disable."
echo -e "\t-m \t memory test size, value/total memory range: 0/<4 1G/<13 2G/>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
@Ansen
Copy link
Author

Ansen commented Jan 31, 2023

运行

快速运行

自动判断是否需要测试内存并设置相应的内存大小

bash <(curl -s -L https://gist.githubusercontent.com/Ansen/e45320205faf5786d3282ac880f20bab/raw/onekey-NeverIdle.sh?123)

高级运行

# 查看脚本帮助
bash <(curl -s -L https://gist.githubusercontent.com/Ansen/e45320205faf5786d3282ac880f20bab/raw/onekey-NeverIdle.sh?123) -h
# 关闭内存测试
bash <(curl -s -L https://gist.githubusercontent.com/Ansen/e45320205faf5786d3282ac880f20bab/raw/onekey-NeverIdle.sh?123) -m 0
# 关闭网络测试
bash <(curl -s -L https://gist.githubusercontent.com/Ansen/e45320205faf5786d3282ac880f20bab/raw/onekey-NeverIdle.sh?123) -n 0

注意

  1. 执行上面的命令,并不会安装除 NeverIdle 二进制文件以外的东西到你的服务器
  2. 若要关闭或卸载 NeverIdle,请参考脚本运行结束后输出的提示

Change log

20230207: 在日志中新增清理命令
20230202:添加参数,支持设置CPU、内存、网络测试的阈值,支持关闭内存和网络测试,CPU测试不允许关闭
20230131:创建脚本

@lulu0024
Copy link

小白问下,如何一键卸载脚本

@Astro-Lee
Copy link

你看脚本执行完毕后的提示

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment