-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkeepalive-light.sh
85 lines (76 loc) · 2.25 KB
/
keepalive-light.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
#!/bin/bash
# default values:
# cpu: 20%
# memory: 20%(just work on arm instance)
# network: 1000K/s
durl="https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-12.0.0-amd64-DVD-1.iso";
ins_opt () {
command -v apt &>/dev/null && ins='apt'
command -v yum &>/dev/null && ins='yum';
command -v dnf &>/dev/null && ins='dnf';
command -v apk &>/dev/null && ins='apk';
[[ "x${ins}" == "x" ]] && exit 233;
${ins} install -y "$1" &>/dev/null || exit 233;
}
set_cpu_net () {
cpuq=$(lscpu | awk '/^CPU\(/{print $NF*20}');
[ -d "/opt/shuaibi" ] || mkdir -p /opt/shuaibi;
cat << eof > /opt/shuaibi/cpu_net.sh;
cpuc=$(lscpu | awk '/^CPU\(/{print $NF}');
for ((i=0;i<cpuc;i++))
do
{
dd if=/dev/zero of=/dev/null
} &
done
while true;
do
curl -skLo /dev/null "${durl}" --limit-rate 1000K;
done
wait
eof
cat << eof > /lib/systemd/system/cpur.service
[Unit]
Description=cpu stress 20 percents & download file with 1000K/s speed
After=network.target
[Service]
Type=simple
ExecStart=/bin/bash /opt/shuaibi/cpu_net.sh
CPUQuota=${cpuq}%
[Install]
WantedBy=multi-user.target
eof
systemctl daemon-reload;
systemctl enable cpur --now;
systemctl restart cpur && echo "settle cpu & network stress succeed.";
}
set_mem () {
if [ ! -d "/opt/shuaibi" ]; then
mkdir -p /opt/shuaibi
fi
if [ -f "/opt/shuaibi/mem.sh" ]; then
echo "/opt/shuaibi/mem.sh already exists."
return
fi
cat << eof > /opt/shuaibi/mem.sh
[ -d '/ramdisk' ] || mkdir -p /ramdisk;
umount /ramdisk &>/dev/null;
mem_count=\$(free -m|awk '/^Mem/{print \$2}');
((mem_use=mem_count*20/100));
mount -t tmpfs -o size=\${mem_use}M tmpfs /ramdisk;
img_size=\$(df -m /ramdisk|awk 'NR>1{print \$2-50}');
dd if=/dev/zero of=/ramdisk/dd.img bs=1M count=\${img_size} &>/dev/null;
eof
/bin/bash /opt/shuaibi/mem.sh && \
cat << eof >> /etc/crontab
@reboot /bin/bash /opt/shuaibi/mem.sh
eof
[[ "$?" == "0" ]] && echo "settle memory stress succeed.";
}
check_env () {
command -v curl &>/dev/null || ins_opt "curl";
(($(id -u)==0)) || exit 233
}
check_env;
[[ "$(uname -m)" == "aarch64" ]] && set_mem;
set_cpu_net;