freedombone-utils-watchdog 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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@freedombone.net>
  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 daemon_is_running {
  48. daemon_name=$1
  49. systemctl is-active ${daemon_name} >/dev/null 2>&1 && echo Running
  50. }
  51. function add_watchdog_daemon {
  52. daemon_name=$1
  53. echo '' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  54. echo "# keep ${daemon_name} daemon running" >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  55. echo "RUNNING=\$(systemctl is-active ${daemon_name} >/dev/null 2>&1 && echo Running)" >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  56. echo 'if [ ! $RUNNING ]; then' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  57. echo " systemctl start ${daemon_name}" >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  58. echo ' echo -n $CURRENT_DATE >> $LOGFILE' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  59. echo " echo \"${daemon_name} daemon restarted\" >> \$LOGFILE" >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  60. echo 'fi' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  61. echo "# End of ${daemon_name}" >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  62. }
  63. function remove_watchdog_daemon {
  64. daemon_name=$1
  65. sed -i "/# keep ${daemon_name} daemon running/,/# End of ${daemon_name}/d" /usr/bin/$WATCHDOG_SCRIPT_NAME
  66. }
  67. # NOTE: deliberately no exit 0