You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.8 KiB

  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2010 segal.di.ubi.pt
  4. #
  5. # This is free software, licensed under the GNU General Public License v2.
  6. #
  7. reboot_now() {
  8. reboot &
  9. [ "$1" -ge 1 ] && {
  10. sleep "$1"
  11. echo 1 > /proc/sys/kernel/sysrq
  12. echo b > /proc/sysrq-trigger # Will immediately reboot the system without syncing or unmounting your disks.
  13. }
  14. }
  15. watchcat_always() {
  16. local period="$1"; local forcedelay="$2"
  17. sleep "$period" && reboot_now "$forcedelay"
  18. }
  19. watchcat_ping() {
  20. local period="$1"; local forcedelay="$2"; local pinghosts="$3"; local pingperiod="$4"; local nopingtime="$5"
  21. local time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
  22. [ "$time_now" -lt "$nopingtime" ] && sleep "$((nopingtime-time_now))"
  23. time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
  24. local time_lastcheck="$time_now"
  25. local time_lastcheck_withinternet="$time_now"
  26. while true
  27. do
  28. # account for the time ping took to return. With a ping time of 5s, ping might take more than that, so it is important to avoid even more delay.
  29. time_now="$(cat /proc/uptime)"; time_now="${time_now%%.*}"
  30. local time_diff="$((time_now-time_lastcheck))"
  31. [ "$time_diff" -lt "$pingperiod" ] && sleep "$((pingperiod-time_diff))"
  32. time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
  33. time_lastcheck="$time_now"
  34. for host in $pinghosts
  35. do
  36. if ping -c 1 "$host" &> /dev/null
  37. then
  38. time_lastcheck_withinternet="$time_now"
  39. else
  40. logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $((time_now-time_lastcheck_withinternet)). Reseting when reaching $period"
  41. fi
  42. done
  43. [ "$((time_now-time_lastcheck_withinternet))" -ge "$period" ] && reboot_now "$forcedelay"
  44. done
  45. }
  46. if [ "$1" = "always" ]
  47. then
  48. watchcat_always "$2" "$3"
  49. else
  50. watchcat_ping "$2" "$3" "$4" "$5" "$6"
  51. fi