]> git.saurik.com Git - apt.git/blame - debian/apt.cron.daily
* apt-pkg/acquire-worker.cc:
[apt.git] / debian / apt.cron.daily
CommitLineData
0c132682
MZ
1#!/bin/sh
2#
0c132682
MZ
3
4#set -e
8a139d4d
MV
5#
6# This file understands the following apt configuration variables:
7#
8# "APT::Periodic::Update-Package-Lists=1"
9# - Do "apt-get update" automatically every n-days (0=disable)
10#
11# "APT::Periodic::Download-Upgradeable-Packages=0",
12# - Do "apt-get upgrade --download-only" every n-days (0=disable)
13#
14# "APT::Periodic::AutocleanInterval"
15# - Do "apt-get autoclean" every n-days (0=disable)
16#
fdd15654
MV
17# "APT::Periodic::Unattended-Upgrade"
18# - Run the "unattended-upgrade" security upgrade script
19# every n-days (0=disabled)
20# Requires the package "unattended-upgrades" and will write
21# a log in /var/log/unattended-upgrades
22#
8a139d4d
MV
23# "APT::Archives::MaxAge",
24# - Set maximum allowed age of a cache package file. If a cache
25# package file is older it is deleted (0=disable)
26#
27# "APT::Archives::MaxSize",
28# - Set maximum size of the cache in MB (0=disable). If the cache
29# is bigger, cached package files are deleted until the size
30# requirement is met (the biggest packages will be deleted
31# first).
32#
33# "APT::Archives::MinAge"
34# - Set minimum age of a package file. If a file is younger it
35# will not be deleted (0=disable). Usefull to prevent races
36# and to keep backups of the packages for emergency.
37#
0c132682 38
05f6a46a 39check_stamp()
0c132682 40{
05f6a46a
MZ
41 stamp="$1"
42 interval="$2"
43
10946ddc
MZ
44 if [ $interval -eq 0 ]; then
45 return 1
46 fi
47
05f6a46a
MZ
48 if [ ! -f $stamp ]; then
49 return 0
0c132682 50 fi
05f6a46a
MZ
51
52 # compare midnight today to midnight the day the stamp was updated
9c2d35fa
MV
53 stamp_file="$stamp"
54 stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null)
55 if [ "$?" != "0" ]; then
56 # Due to some timezones returning 'invalid date' for midnight on
57 # certain dates (eg America/Sao_Paulo), if date returns with error
58 # remove the stamp file and return 0. See coreutils bug:
59 # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
60 rm -f "$stamp_file"
61 return 0
62 fi
63
64 now=$(date --date=$(date --iso-8601) +%s 2>/dev/null)
65 if [ "$?" != "0" ]; then
66 # As above, due to some timezones returning 'invalid date' for midnight
67 # on certain dates (eg America/Sao_Paulo), if date returns with error
68 # return 0.
69 return 0
70 fi
71
05f6a46a 72 delta=$(($now-$stamp))
05f6a46a 73
8a139d4d
MV
74 # intervall is in days,
75 interval=$(($interval*60*60*24))
76 #echo "stampfile: $1"
77 #echo "interval=$interval, now=$now, stamp=$stamp, delta=$delta"
78
05f6a46a
MZ
79 if [ $delta -ge $interval ]; then
80 return 0
81 fi
82
83 return 1
84}
85
86update_stamp()
87{
88 stamp="$1"
89
90 touch $stamp
0c132682
MZ
91}
92
6cce801a
MV
93
94
95# we check here if autoclean was enough sizewise
2e8a92e5 96check_size_constraints()
6cce801a
MV
97{
98 # min-age in days
99 MaxAge=0
89eaeb44 100 MinAge=2
6cce801a
MV
101 MaxSize=0
102 CacheDir="var/cache/apt"
103 CacheArchive="archives/"
104 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
8a139d4d 105 eval $(apt-config shell MinAge APT::Archives::MinAge)
6cce801a 106 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
01717245 107 eval $(apt-config shell Dir Dir)
6cce801a
MV
108 eval $(apt-config shell CacheDir Dir::Cache)
109 eval $(apt-config shell CacheArchive Dir::Cache::archives)
110
111 # sanity check
112 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
113 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
114 exit
115 fi
01717245
MV
116
117 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
6cce801a
MV
118
119 # check age
8a139d4d 120 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
8e29e348 121 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
8a139d4d 122 elif [ ! $MaxAge -eq 0 ]; then
8e29e348 123 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
6cce801a
MV
124 fi
125
126 # check size
127 if [ ! $MaxSize -eq 0 ]; then
8a139d4d
MV
128 # maxSize is in MB
129 MaxSize=$(($MaxSize*1024))
130
131 #get current time
132 now=$(date --date=$(date --iso-8601) +%s)
133 MinAge=$(($MinAge*24*60*60))
134
6cce801a 135 # reverse-sort by mtime
3408b58c 136 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
6cce801a
MV
137 du=$(du -s $Cache)
138 size=${du%%/*}
139 # check if the cache is small enough
140 if [ $size -lt $MaxSize ]; then
141 break
142 fi
8a139d4d
MV
143
144 # check for MinAge of the file
145 if [ ! $MinAge -eq 0 ]; then
8e29e348 146 # check both ctime and mtime
3971f8e8 147 mtime=$(stat -c %Y $file)
8e29e348
MV
148 ctime=$(stat -c %Z $file)
149 if [ $mtime -gt $ctime ]; then
150 delta=$(($now-$mtime))
151 else
152 delta=$(($now-$ctime))
153 fi
8a139d4d
MV
154 #echo "$file ($delta), $MinAge"
155 if [ $delta -le $MinAge ]; then
156 #echo "Skiping $file (delta=$delta)"
3408b58c 157 break
8a139d4d
MV
158 fi
159 fi
160
6cce801a
MV
161 # delete oldest file
162 rm -f $file
163 done
164 fi
165}
166
d047c6da 167# sleep for a random interval of time (default 30min)
604c9ee8
MV
168# (some code taken from cron-apt, thanks)
169random_sleep()
170{
171 RandomSleep=1800
172 eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
173 if [ $RandomSleep -eq 0 ]; then
174 return
175 fi
176 if [ -z "$RANDOM" ] ; then
177 # A fix for shells that do not have this bash feature.
178 RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
179 fi
180 TIME=$(($RANDOM % $RandomSleep))
181 sleep $TIME
182}
183
184# main
185
18d38975
OS
186if ! which apt-config >/dev/null; then
187 exit 0
188fi
87ddfb96 189
0c132682 190UpdateInterval=0
05f6a46a 191DownloadUpgradeableInterval=0
9bd1cf87
MZ
192eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
193AutocleanInterval=$DownloadUpgradeableInterval
0eb7056b 194eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
fdd15654
MV
195UnattendedUpgradeInterval=0
196eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
197
5f71505b
MV
198# check if we actually have to do anything
199if [ $UpdateInterval -eq 0 ] &&
200 [ $DownloadUpgradeableInterval -eq 0 ] &&
201 [ $UnattendedUpgradeInterval -eq 0 ] &&
202 [ $AutocleanInterval -eq 0 ]; then
203 exit 0
204fi
fdd15654 205
0c132682
MZ
206# laptop check, on_ac_power returns:
207# 0 (true) System is on mains power
208# 1 (false) System is not on mains power
209# 255 (false) Power status could not be determined
210# Desktop systems always return 255 it seems
2288cf77 211if which on_ac_power >/dev/null; then
05f6a46a 212 on_ac_power
0c132682
MZ
213 if [ $? -eq 1 ]; then
214 exit 0
215 fi
216fi
217
5f71505b
MV
218# sleep random amount of time to avoid hitting the
219# mirrors at the same time
69c28efc
MV
220random_sleep
221
5f71505b 222# check if we can access the cache
80c96536 223if ! apt-get check -q -q 2>/dev/null; then
5f71505b
MV
224 # wait random amount of time before retrying
225 random_sleep
226 # check again
227 if ! apt-get check -q -q 2>/dev/null; then
228 echo "$0: could not lock the APT cache while performing daily cron job. "
229 echo "Is another package manager working?"
230 exit 1
231 fi
80c96536
MV
232fi
233
1fdd4ac4
MV
234# set the proxy based on the admin users gconf settings
235admin_user=$(getent group admin|cut -d: -f4|cut -d, -f1)
d45c49fc 236if [ -n "$admin_user" ] && [ -x /usr/bin/sudo ] && [ -z "$http_proxy" ] && [ -x /usr/bin/gconftool ]; then
20f6e22e
MV
237 use=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/use_http_proxy 2>/dev/null)
238 host=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/host 2>/dev/null)
239 port=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/port 2>/dev/null)
1fdd4ac4
MV
240 if [ "$use" = "true" ] && [ -n "$host" ] && [ -n "$port" ]; then
241 export http_proxy="http://$host:$port/"
242 fi
243fi
80c96536 244
05f6a46a
MZ
245UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
246if check_stamp $UPDATE_STAMP $UpdateInterval; then
2ce23a2a
MV
247 # check for a new archive signing key (against the master keyring)
248 apt-key net-update
249 # now run the update
5dbdb2e1 250 if apt-get -qq update -o APT::Update::Auth-Failure::="cp /usr/share/apt/apt-auth-failure.note /var/lib/update-notifier/user.d/" 2>/dev/null; then
bf1aaa6c
PS
251 # Could possible test access to '/var/run/dbus/system_bus_socket' has well,
252 # but I'm not sure how stable the internal pipe location is defined as
253 # being; so for the moment just 2>/dev/null . --sladen 2007-09-27
05f6a46a 254 if which dbus-send >/dev/null; then
bf1aaa6c 255 dbus-send --system / app.apt.dbus.updated boolean:true 2>/dev/null || true
05f6a46a 256 fi
c2d0fdd1
MV
257 # now run apt-xapian-index if it is installed to ensure the index
258 # is up-to-date
259 if [ -x /usr/sbin/update-apt-xapian-index ]; then
260 ionice -c3 update-apt-xapian-index -q
261 fi
05f6a46a
MZ
262 update_stamp $UPDATE_STAMP
263 fi
0c132682
MZ
264fi
265
05f6a46a
MZ
266DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
267if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
268 apt-get -qq -d dist-upgrade 2>/dev/null
269 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
0c132682 270fi
9bd1cf87 271
fdd15654
MV
272UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
273if check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
274 unattended-upgrade
275 update_stamp $UPGRADE_STAMP
276fi
277
de15fbae
MV
278AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
279if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
280 apt-get -qq autoclean
281 update_stamp $AUTOCLEAN_STAMP
282fi
283
47536509
MV
284# check cache size
285check_size_constraints