2

By default, unattended-upgrades is trigger by anacron and it only start when running on AC power, and don’t start when running on battery. This beviavor is easy to understand to avoid waste power. Anyway, I wish to allow unattended-upgrades to start even if machine running on battery, because some peoples of my friends, including me, always use their laptop only on battery. The following trick doesn’t work on 16.04 :

Set ANACRON_RUN_ON_BATTERY_POWER=yes in /etc/default/anacron

For Ubuntu 16.04, is there another way to allow anacron, or unattended-upgrades, to start even running on battery ?

The tricks on the following topic doesn't work on 16.04 : Run anacron even when on battery (laptop)

Edit 1:

As requested, I used the command dpkg -L anacron | xargs grep -lwi power 2>/dev/null from the comment of waltinator. Then, I change like this, but no change, unattended-upgrades still doesn't start on battery:

. Comment the following lines in /etc/init.d/anacron

# if test x"$ANACRON_RUN_ON_BATTERY_POWER" != x"yes" && test -x /usr/bin/on_ac_power
# then
#    /usr/bin/on_ac_power >/dev/null
#    if test $? -eq 1
#    then
#      log_progress_msg "deferred while on battery power"
#  log_end_msg 0
#  exit 0
#    fi
#fi
NicolasSmith
  • 1,031

3 Answers3

0

as per https://github.com/mvo5/unattended-upgrades/blob/master/data/50unattended-upgrades.Ubuntu you can add the following option to the file /etc/apt/apt.conf.d/50unattended-upgrades:

Unattended-Upgrade::OnlyOnACPower "false";

check with sudo unattended-upgrades --verbose if it works

Martin
  • 1
0

After searching for a long time to allow anacron to start even on battery, I give up. Because I found a script to start unattended-upgrades if machine running on battery, which I originally wanted.

Run the unattended-upgrades if machine running on battery, perfect for laptop. Script to be execute at login. Requirement :

  • Unattended-upgrades installed and enabled (link)
  • Allow unattended-upgrades and apt update to be execute without password prompt with sudoers (link)
  • Run script at login (link)

Script :

#!/bin/bash
sleep 6m
level=$(cat /sys/class/power_supply/BAT0/capacity)
status=$(cat /sys/class/power_supply/BAT0/status)
lastupdate=$(cat /var/log/unattended-upgrades/unattended-upgrades.log | grep `date -I` | tail -1)
# Exit if not discharging
if [ "${status}" != "Discharging" ]; then
  exit 0
fi
# Exit if updated today
if [ -n "$lastupdate" ]; then
  exit 0
fi
# Update
if [ "${level}" -ge 70 ]; then  
    sudo apt update && sudo unattended-upgrades
fi
derHugo
  • 3,356
  • 5
  • 31
  • 51
NicolasSmith
  • 1,031
0

On 16.04, you can write a systemd file to achieve this. Create the file /etc/systemd/system/anacron.service.d/override.conf with the following content:

[Unit]
ConditionACPower=
ConditionACPower=false

Note that some cronjobs independently check for power source and decline to run on battery power, e.g. /etc/cron.daily/mlocate

Nick
  • 1