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.

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