Bash http probe

Posted on September 3, 2009

A Little bash script that can notify whenever a site you own goes down.

Of course there are more sofisticated ways to achieve better results but this script is simple enough to be set up in a minute. You can pass a file containing all the sites to check with the -f flag, this is useful when used in a crontab for instance, instead if you want to rapidly do a loop on the command line you can pass a single site with the -s flag.

  1. home='/path/to/homefolder'
  2. targetfile="$home/Desktop/SITES_NOT_RESPONDING"
  3.  
  4. function checksite(){
  5. HEAD "$1" > "$home/output"
  6. grep "^4[0-9][0-9]" < "$home/output" >> "$home/errors"
  7. grep "^5[0-9][0-9]" < "$home/output" >> "$home/errors"
  8.  
  9. if [ -s errors ]; then
  10. /usr/bin/beep -f 1800 -l 200 -r 4 -d 500
  11. echo "$1" >> "$targetfile"
  12. cat "$home/errors" >> "$targetfile"
  13. echo '' >> "$targetfile"
  14. notify-send -t 5000 "$1 is down!"
  15. rm "$home/output" "$home/errors"
  16. fi
  17. }
  18.  
  19. function loopfile(){
  20. while read line
  21. do
  22. checksite "$line"
  23. done < "$1"
  24. }
  25.  
  26. while getopts s:f: options
  27. do
  28. case $options in
  29. f) loopfile $OPTARG;;
  30. s) checksite $OPTARG;;
  31. esac
  32. done


Get the source code here.

Browse the archive

blog comments powered by Disqus