1. CPU占用
脚本名:cup.sh
#!/bin/bash
endless_loop() {
echo -ne "i=0; while true ;do i=i+100; i=100;done" | /usr/bin/bash &
}
if [ $# != 2 ]; then
echo "USAGE: $0 <cpus,sleep time>"
exit 1
fi
for i in $(seq $1); do
endless_loop
pid_array[$i]=$!
done
for i in "${pid_array[@]}"; do
echo 'execute: kill' $i
echo 'kill' $i >>/tmp/cpufile.txt
done
echo "If executed ctrl+C,Please execute the above lines manually"
echo "Please wait $2 seconds"
sleep $2
for i in $(awk '{print $2}' /tmp/cpufile.txt); do
echo "kill $i"
kill $i
done
find /tmp -name cpufile.txt -delete
调用方式
sh cpu.sh 3 100
2. 内存占用
memory.sh
#!/bin/bash
if [ $# != 2 ]; then
echo "USAGE: $0 <free,sleep time>"
exit 1
fi
free -g
[ -d /tmp/memory ] && exit 1 || mkdir /tmp/memory
mount -t tmpfs -o size=$1G tmpfs /tmp/memory
dd if=/dev/zero of=/tmp/memory/block
free -g
echo "If executed ctrl+C,Please execute the following lines manually"
echo "execute: rm -rf /tmp/memory/block"
echo "execute: umount /tmp/memory"
echo "execute: rmdir /tmp/memory"
echo "Please wait $2 seconds"
sleep $2
find /tmp/memory -name block -delete
umount /tmp/memory
find /tmp -name memory -delte
调用方式
sh memory.sh 3 100
crontab执行示例
*/15 19-23,0-7 * * * nohup /bin/bash /data/scripts/cpu.sh 3 100 > /dev/null &
*/30 8-18 * * * nohup /bin/bash /data/scripts/cpu.sh 1 80 > /dev/null &
*/20 19-23,0-7 * * * nohup /bin/bash /data/scripts/memory.sh 5 300 > /dev/null &