Sometimes it is necessary to check whether server you want to run some big bash script is connected to Internet. Usually it makes sense while running scripts periodically using cron.  Below is the tiny bash script for this purpose:



$WGET -q --tries=10 --timeout=5 -O /tmp/ &> /dev/null
if [ ! -s /tmp/ ];then
	echo "no"
	echo "yes"

As you see it tries to download google’s index page, if it’s not empty script returns “yes”, if there is not Internet connection available script will return “no”. If it is impossible to fetch the page in more than 5 seconds script will return “no” as well.

Anything to add? You are welcome! :)




Leave a reply


Your email address will not be published.