80后的细娃儿

OpenWRT路由器中監控網絡服務并重啟的腳本

在/root下編寫一個my_watchdog.sh腳本:

#!/bin/sh
#sleep 100
DATE=date +%Y-%m-%d-%H:%M:%S
tries=0
echo — my_watchdog start —
while [[ $tries -lt 5 ]]
do
if /bin/ping -c 1 8.8.8.8 >/dev/null
then
echo — exit —
# echo $DATE OK >>my_watchdog.log
exit 0
fi
tries=$((tries+1))
sleep 10
# echo $DATE tries: $tries >>my_watchdog.log
done

echo $DATE network restart >>my_watchdog.log
/etc/init.d/network restart

#echo $DATE reboot >>my_watchdog.log
#reboot
  然後用crontab -e來在系統的crontab中添加一句,每隔5分鐘定時運行這個腳本:

*/5 * * * * sh /root/my_watchdog.sh

0 0,4,8,12,16,20 * * * date -u +”%Y.%m.%d-%H:%M:%S” >/usr/data/time_backup
0,1,11,21,31,41,51 * * * * /usr/bin/set_kernel_timezone >/dev/null 2>&1
0 0,4,8,12,16,20 * * * /tmp/bw_backup/do_bw_backup.sh

点赞

发表评论