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.

74 lines
1.7 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. mode="$1"
  8. shutdown_now() {
  9. local forcedelay="$1"
  10. reboot &
  11. [ "$forcedelay" -ge 1 ] && {
  12. sleep "$forcedelay"
  13. echo b > /proc/sysrq-trigger # Will immediately reboot the system without syncing or unmounting your disks.
  14. }
  15. }
  16. watchcat_allways() {
  17. local period="$1"; local forcedelay="$2"
  18. sleep "$period" && shutdown_now "$forcedelay"
  19. }
  20. watchcat_ping() {
  21. local period="$1"; local forcedelay="$2"; local pinghosts="$3"; local pingperiod="$4"
  22. time_now="$(cat /proc/uptime)"
  23. time_now="${time_now%%.*}"
  24. time_lastcheck="$time_now"
  25. 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)"
  30. time_now="${time_now%%.*}"
  31. time_diff="$((time_now-time_lastcheck))"
  32. [ "$time_diff" -lt "$pingperiod" ] && {
  33. sleep_time="$((pingperiod-time_diff))"
  34. sleep "$sleep_time"
  35. }
  36. time_now="$(cat /proc/uptime)"
  37. time_now="${time_now%%.*}"
  38. time_lastcheck="$time_now"
  39. for host in "$pinghosts"
  40. do
  41. if ping -c 1 "$host" &> /dev/null
  42. then
  43. time_lastcheck_withinternet="$time_now"
  44. else
  45. time_diff="$((time_now-time_lastcheck_withinternet))"
  46. logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $time_diff seconds. Reseting when reaching $period"
  47. fi
  48. done
  49. time_diff="$((time_now-time_lastcheck_withinternet))"
  50. [ "$time_diff" -ge "$period" ] && shutdown_now "$forcedelay"
  51. done
  52. }
  53. if [ "$mode" = "allways" ]
  54. then
  55. watchcat_allways "$2" "$3"
  56. else
  57. watchcat_ping "$2" "$3" "$4" "$5"
  58. fi