shell
shell 判断 命令执行情况
https://blog.csdn.net/Maybe_Lee/article/details/78604927
脚本例子:
1、 删除垃圾文件
#!/bin/bash cache_file=$(find . -name "*~") cache_file+=' ' cache_file+=$(find . -name "*.swp") cache_file+=' ' cache_file+=$(find . -name "*.py[co]") cache_file+=' ' cache_file+=$(find . -name "*.bin") cache_file+=' ' cache_file+=$(find . -name "*.log") cache_file+=' ' cache_file+=$(find . -name "*.png") cache_file+=' tmp/* !(tmp/.gitignore)' for cache in ${cache_file} do echo "Deleting file ${cache}" && rm -rf ${cache} done
使用方法:
可以在/etc/rc.local 中添加命令:
sh /root/cpu_test/test_reboot.sh &
两个脚本要放在同一文件夹下。注意文件位置。
主脚本中的“4” 就是开机次数。可以自行修改。
主脚本:
#! /bin/bash # filename: test_reboot.sh cores=`cat /proc/cpuinfo| grep "cpu cores" | awk -F ' ' '{print $4}'| uniq` echo `date +"%Y-%m-%d %H:%M:%S"` "power on" >> /root/cpu_test/reboot.log sleep 1 A=`cat /root/cpu_test/reboot.log | grep "power on" | wc -l` if [ $A -gt "4" ] then echo "power on " $A "times" >> /root/cpu_test/reboot.log else sleep 30 sh /root/cpu_test/all_cpu.sh $cores & sleep 1m reboot fi
cpu满载脚本:
#! /bin/bash # filename: all_cpu.sh if [ $# != 1 ] ; then echo "USAGE: $0 <CPUs>" exit 1; fi for i in `seq $1` do echo -ne " i=0; while true do i=i+1; done" | /bin/sh & pid_array[$i]=$! ; done for i in "${pid_array[@]}"; do echo 'kill ' $i ';'; done