#!/bin/bash # Description: Check every 5 minutes whether it is connected to the internet or not # and store the result into a file. iconnectionDir=/root/iconnection mkdir ${iconnectionDir} let SecondsInOneDay=60*60*24 # Predefined startup variables. TimeInSeconds=`date +%s` CurrentDate=`date +%Y-%m-%d_%H.%M.%S` InternetConnectionTest="InternetConnectionTest_"$CurrentDate".txt" # Infinite loop while( true ) do sleep 5m #--------------------------------------------------------------------------- # Store the internet connection status of the whole day into 1 file per day. # Update the output filename if the number of elapsed seconds is greater than 1 day. #--------------------------------------------------------------------------- CurrentTimeInSeconds=`date +%s` let DifferenceInSeconds=$CurrentTimeInSeconds-$TimeInSeconds # Change the output filename if the number of elapsed seconds is greater than 1 day. if [ $DifferenceInSeconds -gt $SecondsInOneDay ] then CurrentDate=`date +%Y-%m-%d_%H.%M.%S` InternetConnectionTest="InternetConnectionTest_"$CurrentDate".txt" TimeInSeconds=$CurrentTimeInSeconds fi #-------------------------------------------------- # Test internet connection. #-------------------------------------------------- # Ping timeout = 5 seconds if ping -c1 -w 5 -q 4.2.2.2 then echo "`date +%Y-%m-%d_%H.%M.%S` Connected" >> ${iconnectionDir}/$InternetConnectionTest else echo "`date +%Y-%m-%d_%H.%M.%S` Disconnected" >> ${iconnectionDir}/$InternetConnectionTest fi done exit 0