Check connectivity script

This past weekend I had some issues with my ISP.
So for future purpose I'm going to have some logging on my internet so I'm able to attach log files to my complaint email if this issue reoccurs.

Decided to do a simple ping script that runs every 5 or 10 min with crontab
if ping fail write date followed by ping result to pinglog.txt

Everything works except geting ping result into pinglog.txt
Script just prints to terminal.
I have tested the line in terminal and then result prints to pinglog.txt.

Script and pinglog.txt are both in /home/seb/bin
date gets printed into pinglog.txt
123google.com is intended for if to be true

#!/bin/bash

targetHost="123google.com"

/bin/ping -c 1 $targetHost
if [ $? != 0 ]
        then
echo    "Internet: DOWN `date`" >> pinglog.txt

        /bin/ping -c 1 $targetHost >> pinglog.txt

else
        echo "Internet: OK"
fi

---------- Post updated at 03:52 ---------- Previous update was at 03:42 ----------

nvm found my error

/bin/ping -c 1 $targetHost 2>> /home/seb/bin/pinglog.txt

To be more clear, the problem could have solved by 2 things.

  1. Redirection of stderror:
    As in error situation we need to filter the stderr as 2.

  2. Absolute path for output file.
    We need to give the absolute path for redirection, especially in batch processes.

As I'm writing this I'm currently building the script. I'm at the office so it's not going so fast since I get interrupted all the time.
First version was just to get everything working in my bash terminal. When I'm done there and the pinglog.txt looks good. Then I'm editing it for cron.

Just finished adding absolute path's for all commands.

Maybe using nmap to check a webserver's port etc. is up instead of just ping'ing it might be also helpful (no idea if ping check is sufficient - just as an idea to add).

PING might not be helpful all the time, site might be up but replying back to control message protocol might be denied, in that case ping will not get a reply

I decided to remake the script so its less lines..
Right now It looks like this

#!/bin/bash

targetHost="
www.123sunet.se
www.123arla.se
www.123dn.se
"
count=0

for x in $targetHost
do
	if  /bin/ping -c1 -w1 $x $1>/dev/null 
	then
	echo "ok">/dev/null
	else
		let ++count 		

	fi
done
 
echo $count
if [ $count -eq 3 ]
then
	for x in $targetHost
	do
		/bin/echo "Internet: DOWN `date` $x" >> /home/seb/bin/pinglog.txt
		/bin/ping -c 1 $x 12>> /home/seb/bin/pinglog.txt
		/bin/echo "--------------------------------------------" >> /home/seb/bin/pinglog.txt
	done
else
	echo "Do nothing everything works"
fi

Here is pinglog.txt

seb@seb-VM:~/bin$ cat pinglog.txt 
Internet: DOWN Wed Nov  4 20:27:02 CET 2009 www.123sunet.se
--------------------------------------------
Internet: DOWN Wed Nov  4 20:27:02 CET 2009 www.123arla.se
--------------------------------------------
Internet: DOWN Wed Nov  4 20:27:02 CET 2009 www.123dn.se
--------------------------------------------
seb@seb-VM:~/bin$