apt.apt-compat.cron.daily 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/sh
  2. set -e
  3. # Systemd systems use a systemd timer unit which is preferable to
  4. # run. We want to randomize the apt update and unattended-upgrade
  5. # runs as much as possible to avoid hitting the mirrors all at the
  6. # same time. The systemd time is better at this than the fixed
  7. # cron.daily time
  8. if [ -d /run/systemd/system ]; then
  9. exit 0
  10. fi
  11. check_power()
  12. {
  13. # laptop check, on_ac_power returns:
  14. # 0 (true) System is on main power
  15. # 1 (false) System is not on main power
  16. # 255 (false) Power status could not be determined
  17. # Desktop systems always return 255 it seems
  18. if which on_ac_power >/dev/null 2>&1; then
  19. on_ac_power
  20. POWER=$?
  21. if [ $POWER -eq 1 ]; then
  22. return 1
  23. fi
  24. fi
  25. return 0
  26. }
  27. # sleep for a random interval of time (default 30min)
  28. # (some code taken from cron-apt, thanks)
  29. random_sleep()
  30. {
  31. RandomSleep=1800
  32. eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
  33. if [ $RandomSleep -eq 0 ]; then
  34. return
  35. fi
  36. if [ -z "$RANDOM" ] ; then
  37. # A fix for shells that do not have this bash feature.
  38. RANDOM=$(( $(dd if=/dev/urandom bs=2 count=1 2> /dev/null | cksum | cut -d' ' -f1) % 32767 ))
  39. fi
  40. TIME=$(($RANDOM % $RandomSleep))
  41. sleep $TIME
  42. }
  43. # delay the job execution by a random amount of time
  44. random_sleep
  45. # ensure we don't do this on battery
  46. check_power || exit 0
  47. # run daily job
  48. exec /usr/lib/apt/apt.systemd.daily