X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/ba8df30dde105e214a9d28edbcf13bfd99a84c2b..92fcbfc16396d9a2fbde0edb0902d4ebe7ff0090:/debian/apt.cron.daily diff --git a/debian/apt.cron.daily b/debian/apt.cron.daily index 1d4df56b6..5c5ca88c7 100644 --- a/debian/apt.cron.daily +++ b/debian/apt.cron.daily @@ -50,8 +50,25 @@ check_stamp() fi # compare midnight today to midnight the day the stamp was updated - stamp=$(date --date=$(date -r $stamp --iso-8601) +%s) - now=$(date --date=$(date --iso-8601) +%s) + stamp_file="$stamp" + stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null) + if [ "$?" != "0" ]; then + # Due to some timezones returning 'invalid date' for midnight on + # certain dates (eg America/Sao_Paulo), if date returns with error + # remove the stamp file and return 0. See coreutils bug: + # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html + rm -f "$stamp_file" + return 0 + fi + + now=$(date --date=$(date --iso-8601) +%s 2>/dev/null) + if [ "$?" != "0" ]; then + # As above, due to some timezones returning 'invalid date' for midnight + # on certain dates (eg America/Sao_Paulo), if date returns with error + # return 0. + return 0 + fi + delta=$(($now-$stamp)) # intervall is in days, @@ -147,16 +164,44 @@ check_size_constraints() fi } +# sleep for a random interval of time (default 30min) +# (some code taken from cron-apt, thanks) +random_sleep() +{ + RandomSleep=1800 + eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep) + if [ $RandomSleep -eq 0 ]; then + return + fi + if [ -z "$RANDOM" ] ; then + # A fix for shells that do not have this bash feature. + RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5") + fi + TIME=$(($RANDOM % $RandomSleep)) + sleep $TIME +} + +# main + +if ! which apt-config >/dev/null; then + exit 0 +fi UpdateInterval=0 DownloadUpgradeableInterval=0 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages) AutocleanInterval=$DownloadUpgradeableInterval eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval) - UnattendedUpgradeInterval=0 eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade) +# check if we actually have to do anything +if [ $UpdateInterval -eq 0 ] && + [ $DownloadUpgradeableInterval -eq 0 ] && + [ $UnattendedUpgradeInterval -eq 0 ] && + [ $AutocleanInterval -eq 0 ]; then + exit 0 +fi # laptop check, on_ac_power returns: # 0 (true) System is on mains power @@ -170,19 +215,28 @@ if which on_ac_power >/dev/null; then fi fi -# check if we can lock the cache and if the cache is clean +# sleep random amount of time to avoid hitting the +# mirrors at the same time +random_sleep + +# check if we can access the cache if ! apt-get check -q -q 2>/dev/null; then - echo "$0: could not lock the APT cache" - exit 1 + # wait random amount of time before retrying + random_sleep + # check again + if ! apt-get check -q -q 2>/dev/null; then + echo "$0: could not lock the APT cache while performing daily cron job. " + echo "Is another package manager working?" + exit 1 + fi fi - UPDATE_STAMP=/var/lib/apt/periodic/update-stamp if check_stamp $UPDATE_STAMP $UpdateInterval; then if apt-get -qq update 2>/dev/null; then - if which dbus-send >/dev/null; then - dbus-send --system / app.apt.dbus.updated boolean:true - fi + if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then + dbus-send --system / app.apt.dbus.updated boolean:true + fi update_stamp $UPDATE_STAMP fi fi