freedombone-utils-cron 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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@robotics.uk.to>
  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. echo "*/${1} * * * * root ${2}" >> /etc/crontab
  33. systemctl restart cron
  34. fi
  35. }
  36. function randomize_cron {
  37. # The predictable default timing of Debian cron jobs might
  38. # be exploitable knowledge. Avoid too much predictability
  39. # by randomizing the times when cron jobs run
  40. if grep -Fxq "randomize_cron" $COMPLETION_FILE; then
  41. return
  42. fi
  43. # randomize the day on which the weekly cron job runs
  44. randdow=$(($RANDOM%6+1))
  45. sed -i "s|\* \* 7|* * $randdow|g" /etc/crontab
  46. # randomize the time when the weekly cron job runs
  47. randmin=$(($RANDOM%60))
  48. randhr=$(($RANDOM%3+1))
  49. sed -i "s|47 6|$randmin $randhr|g" /etc/crontab
  50. # randomize the time when the daily cron job runs
  51. randmin=$(($RANDOM%60))
  52. randhr=$(($RANDOM%3+4))
  53. sed -i "s|25 6\t\* \* \*|$randmin $randhr\t* * *|g" /etc/crontab
  54. # randomize the time when the hourly cron job runs
  55. randmin=$(($RANDOM%60))
  56. sed -i "s|17 \*\t|$randmin *\t|g" /etc/crontab
  57. # randomize monthly cron job time and day
  58. randmin=$(($RANDOM%60))
  59. randhr=$(($RANDOM%22+1))
  60. randdom=$(($RANDOM%27+1))
  61. sed -i "s|52 6\t|$randmin $randhr\t|g" /etc/crontab
  62. sed -i "s|\t1 \* \*|\t$randdom * *|g" /etc/crontab
  63. systemctl restart cron
  64. echo 'randomize_cron' >> $COMPLETION_FILE
  65. }
  66. # NOTE: deliberately there is no "exit 0"