Linux · 2009-01-25

shell编程入门_shell监控硬盘

除夕夜,希望大家吃好喝好!

shell监控硬盘的使用状况,超过89%将会发生警报,并写进check_HD.log文件里。

#!/bin/sh
#a1=$(df -h|sed ‘1d’|awk ‘{print $5}’|sed ‘s/%//g’)
a1=$(df -h|sed ‘1d’|awk ‘{print $5}’|sed ‘s/%//g’|sed -n 1p)
a2=$(df -h|sed ‘1d’|awk ‘{print $5}’|sed ‘s/%//g’|sed -n 2p)
a3=$(df -h|sed ‘1d’|awk ‘{print $5}’|sed ‘s/%//g’|sed -n 3p)
a4=$(df -h|sed ‘1d’|awk ‘{print $5}’|sed ‘s/%//g’|sed -n 4p)
b1=$(df -h|sed 1d|awk ‘{print $1}’|sed -n 1p)
b2=$(df -h|sed 1d|awk ‘{print $1}’|sed -n 2p)
b3=$(df -h|sed 1d|awk ‘{print $1}’|sed -n 3p)
b4=$(df -h|sed 1d|awk ‘{print $1}’|sed -n 4p)
DAY=$(date +%F\ %T)
if [ $a1 -gt 89 ];then
echo “$DAY $b1 would be near 90% !” >> /root/check_HD.log
if [ $a2 -gt 89 ];then
echo “$DAY $b21 would be near 90% !” >> /root/check_HD.log
if [ $a3 -gt 89 ];then
echo “$DAY $b3 would be near 90% !” >> /root/check_HD.log
if [ $a4 -gt 89 ];then
echo “$DAY $b4 would be near 90% !” >> /root/check_HD.log
fi fi fi fi

crontab五分钟调用一次。


# m h dom mon dow command
#@hourly /bin/rsync.php
0-59 * * * * /root/check_apache_running.sh
5 * * * * /root/check_HD.sh