apt.cron.daily 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/sh
  2. #
  3. #set -e
  4. check_stamp()
  5. {
  6. stamp="$1"
  7. interval="$2"
  8. if [ $interval -eq 0 ]; then
  9. return 1
  10. fi
  11. if [ ! -f $stamp ]; then
  12. return 0
  13. fi
  14. # compare midnight today to midnight the day the stamp was updated
  15. stamp=$(date --date=$(date -r $stamp --iso-8601) +%s)
  16. now=$(date --date=$(date --iso-8601) +%s)
  17. delta=$(($now-$stamp))
  18. if [ $delta -ge $interval ]; then
  19. return 0
  20. fi
  21. return 1
  22. }
  23. update_stamp()
  24. {
  25. stamp="$1"
  26. touch $stamp
  27. }
  28. # we check here if autoclean was enough sizewise
  29. check_size_constrains()
  30. {
  31. # min-age in days
  32. MaxAge=0
  33. MaxSize=0
  34. CacheDir="var/cache/apt"
  35. CacheArchive="archives/"
  36. eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  37. eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  38. eval $(apt-config shell CacheDir Dir::Cache)
  39. eval $(apt-config shell CacheArchive Dir::Cache::archives)
  40. # sanity check
  41. if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
  42. echo "empty Dir::Cache or Dir::Cache::archives, exiting"
  43. exit
  44. fi
  45. Cache="/"$CacheDir$CacheArchive
  46. # check age
  47. if [ ! $MaxAge -eq 0 ]; then
  48. find $Cache -name "*.deb" -mtime +$MaxAge -exec rm -f {} \;
  49. fi
  50. # check size
  51. if [ ! $MaxSize -eq 0 ]; then
  52. # reverse-sort by mtime
  53. for file in $(ls -rt $Cache/*.deb); do
  54. du=$(du -s $Cache)
  55. size=${du%%/*}
  56. # check if the cache is small enough
  57. if [ $size -lt $MaxSize ]; then
  58. break
  59. fi
  60. # delete oldest file
  61. rm -f $file
  62. done
  63. fi
  64. }
  65. check_size_constrains
  66. UpdateInterval=0
  67. DownloadUpgradeableInterval=0
  68. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  69. AutocleanInterval=$DownloadUpgradeableInterval
  70. eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean)
  71. # laptop check, on_ac_power returns:
  72. # 0 (true) System is on mains power
  73. # 1 (false) System is not on mains power
  74. # 255 (false) Power status could not be determined
  75. # Desktop systems always return 255 it seems
  76. if which on_ac_power >/dev/null; then
  77. on_ac_power
  78. if [ $? -eq 1 ]; then
  79. exit 0
  80. fi
  81. fi
  82. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  83. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  84. if apt-get -qq update 2>/dev/null; then
  85. if which dbus-send >/dev/null; then
  86. dbus-send --system / app.apt.dbus.updated boolean:true
  87. fi
  88. update_stamp $UPDATE_STAMP
  89. fi
  90. fi
  91. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  92. if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  93. apt-get -qq -d dist-upgrade 2>/dev/null
  94. update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  95. fi
  96. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  97. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  98. apt-get -qq autoclean
  99. check_size_contrains
  100. update_stamp $AUTOCLEAN_STAMP
  101. fi