freedombone-utils-watchdog 2.6KB

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