freedombone-utils-cron 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Cron functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 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. function cron_add_mins {
  31. if ! grep -q "${2}" /etc/crontab; then
  32. job_user='root'
  33. if [ $3 ]; then
  34. job_user=$3
  35. fi
  36. echo "*/${1} * * * * ${job_user} ${2}" >> /etc/crontab
  37. systemctl restart cron
  38. fi
  39. }
  40. function randomize_cron {
  41. # The predictable default timing of Debian cron jobs might
  42. # be exploitable knowledge. Avoid too much predictability
  43. # by randomizing the times when cron jobs run
  44. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  45. return
  46. fi
  47. # randomize the day on which the weekly cron job runs
  48. randdow=$(($RANDOM%6+1))
  49. sed -i "s|\* \* 7|* * $randdow|g" /etc/crontab
  50. # randomize the time when the weekly cron job runs
  51. randmin=$(($RANDOM%60))
  52. randhr=$(($RANDOM%3+1))
  53. sed -i "s|47 6|$randmin $randhr|g" /etc/crontab
  54. # randomize the time when the daily cron job runs
  55. randmin=$(($RANDOM%60))
  56. randhr=$(($RANDOM%3+4))
  57. sed -i "s|25 6\t\* \* \*|$randmin $randhr\t* * *|g" /etc/crontab
  58. # randomize the time when the hourly cron job runs
  59. randmin=$(($RANDOM%60))
  60. sed -i "s|17 \*\t|$randmin *\t|g" /etc/crontab
  61. # randomize monthly cron job time and day
  62. randmin=$(($RANDOM%60))
  63. randhr=$(($RANDOM%22+1))
  64. randdom=$(($RANDOM%27+1))
  65. sed -i "s|52 6\t|$randmin $randhr\t|g" /etc/crontab
  66. sed -i "s|\t1 \* \*|\t$randdom * *|g" /etc/crontab
  67. systemctl restart cron
  68. mark_completed $FUNCNAME
  69. }
  70. function schedule_stig_tests {
  71. echo '#!/bin/bash' > /etc/cron.daily/stig_tests
  72. echo "pkill ${PROJECT_NAME}-tests" >> /etc/cron.daily/stig_tests
  73. echo 'rm -rf /tmp/*' >> /etc/cron.daily/stig_tests
  74. echo '${PROJECT_NAME}-tests --stig yes' >> /etc/cron.daily/stig_tests
  75. chmod +x /etc/cron.daily/stig_tests
  76. }
  77. # NOTE: deliberately there is no "exit 0"