[root@Localhost ~]# vi ping_check.sh
#!/bin/bash
DATE=$(date +"%d.%m.%Y %T")
SHORT_DATE=$(date +"%d.%m.%Y")
echo
echo "Pinging host " $@
echo
HOST=$@
ping $HOST | while read PONG
do
grep -q ttl <<< "$PONG"
if [ $? -eq 0 ]; then
echo "`date`: $PONG"
echo "`date`: $PONG" &>> ping_check_$SHORT_DATE.log
else
echo "`date`: ping failed, $HOST host is DOWN!" &>> ping_check_$SHORT_DATE.log
echo "$PONG" &>> ping_check_$SHORT_DATE.log
fi
done
[root@Localhost ~]#chmod +x ping_check.sh
[root@Localhost ~]#sh ping_check.sh google.com
執行後的Log格式顯示如下
[root@Localhost ~]#sh ping_check.sh google.com
執行後的Log格式顯示如下
若系統帳號登出後,要再背景執行的話,要再加上nohup指令執行即可
[root@Localhost ~]#nohup sh ping_check.sh google.com &
[root@Localhost ~]#nohup sh ping_check.sh google.com &
0 回應:
張貼留言