freedombone-utils-watchdog 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Watchdog functions to keep systems running even if they crash
  12. # systemd itself can handle this, but some debian packages are in
  13. # twilight world where they still use the older init scripts
  14. #
  15. # License
  16. # =======
  17. #
  18. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  19. #
  20. # This program is free software: you can redistribute it and/or modify
  21. # it under the terms of the GNU Affero General Public License as published by
  22. # the Free Software Foundation, either version 3 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU Affero General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU Affero General Public License
  31. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. # name of a script which keeps running processes going even if they crash
  33. WATCHDOG_SCRIPT_NAME="keepon"
  34. function install_watchdog_script {
  35. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  36. return
  37. fi
  38. echo '#!/bin/bash' > /usr/bin/$WATCHDOG_SCRIPT_NAME
  39. echo 'LOGFILE=/var/log/keepon.log' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  40. echo 'CURRENT_DATE=$(date)' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  41. # application specific stuff is added later
  42. chmod +x /usr/bin/$WATCHDOG_SCRIPT_NAME
  43. function_check cron_add_mins
  44. cron_add_mins 1 "/usr/bin/$WATCHDOG_SCRIPT_NAME"
  45. mark_completed $FUNCNAME
  46. }
  47. function add_watchdog_daemon {
  48. daemon_name=$1
  49. echo '' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  50. echo "# keep ${daemon_name} daemon running" >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  51. echo "RUNNING=$(pgrep ${daemon_name} > /dev/null && echo Running)" >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  52. echo 'if [ ! $RUNNING ]; then' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  53. echo " systemctl start ${daemon_name}" >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  54. echo ' echo -n $CURRENT_DATE >> $LOGFILE' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  55. echo " echo \"${daemon_name} daemon restarted\" >> \$LOGFILE" >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  56. echo 'fi' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  57. echo "# End of ${daemon_name}" >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  58. }
  59. function remove_watchdog_daemon {
  60. daemon_name=$1
  61. sed -i "/# keep ${daemon_name} daemon running/,/# End of ${daemon_name}/d" /usr/bin/$WATCHDOG_SCRIPT_NAME
  62. }
  63. # NOTE: deliberately no exit 0