Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[SOLVED] How to: easy updates on a PC ?

If none of the specific sub-forums seem right for your thread, ask here.
Message
Author
User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

[SOLVED] How to: easy updates on a PC ?

#1 Post by f.r3d »

Hi everyone!

I installed Debian 8 Mate on two computers (my parents' computers). For the moment I update the computers through Teamviewer and I would like to find a solution so that the updates would be installed automatically, without my intervention. I already looked at cron-apt and unattended-upgrades but there is one detail that I can't figure out: how to launch the "update checker & installer" at each startup? I know that cron-apt and unattended-upgrades can check for updates once a day at a certain time, this is useful for a server, but I can't predict when my parents will turn on their computers.

So what should I use: cron-apt, unattended-upgrades, a bash script?

Thank you for your help!
Last edited by f.r3d on 2017-06-10 15:22, edited 2 times in total.
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

User avatar
alan stone
Posts: 269
Joined: 2011-10-22 14:08
Location: In my body.

Re: How to : easy updates on a PC ?

#2 Post by alan stone »

For your information: see here, here and here.

> So what should I use: cron-apt, unattended-upgrades, a bash script?

The one which you're most comfortable with.
Last edited by alan stone on 2017-01-05 09:33, edited 1 time in total.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: How to : easy updates on a PC ?

#3 Post by pylkko »

Systemd timers can be used to start any process upon boot or shutdown or even when idle. So you could have an update service run once a week when the computer is first turned on. I believe you can also do this with anacron, but with cron the problem is that you will not (as you say) know when the machines are on or not.

A work around would be to use a daily update as you state & choose a time when the machines are most likely on. If the computers are used even a few times a week the chances are really small that updates would be missed for a significant time period.

User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

Re: How to : easy updates on a PC ?

#4 Post by f.r3d »

Well I would rather use unattended-upgrades but I don't know how how to launch when the system starts. I have already looked at all the available documentation online but I could not find anything about launching unattended-upgrades at start time. So I guess I'll have to use unattended-upgrades and something else for it to work how I want to. OR I could write a bash script that could update the computer automatically at startup, something like:

Code: Select all

#!/bin/bash
sudo apt-get update
sudo apt-get upgrade -y
But I don't know how to launch it automatically at startup and how to make it work without typing the sudo password. Any idea?
Last edited by f.r3d on 2017-03-01 08:28, edited 1 time in total.
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

Re: How to : easy updates on a PC ?

#5 Post by f.r3d »

So I decided to use anacron.

I created a script in /etc/cron.daily/automatic_updates

Code: Select all

#!/bin/bash
sudo apt-get update
sudo apt-get upgrade -y
then I used

Code: Select all

sudo chmod 755 /etc/cron.daily/automatic_updates 
I edited /etc/anacrontab and added the line

Code: Select all

@daily  5       automatic_updates        /etc/cron.daily/automatic_updates
EDIT : I found this http://askubuntu.com/a/425762
So in order to enable the system to run my bash script without asking me for password, I did:

Code: Select all

sudo visudo
an then I added

Code: Select all

[myusername] ALL=(ALL) NOPASSWD: /etc/cron.daily/automatic_updates
Is it correct ?
Last edited by f.r3d on 2017-05-09 17:26, edited 7 times in total.
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

User avatar
alan stone
Posts: 269
Joined: 2011-10-22 14:08
Location: In my body.

Re: How to : easy updates on a PC ?

#6 Post by alan stone »


User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

Re: How to : easy updates on a PC ?

#7 Post by f.r3d »

wizard10000 wrote:anacron jobs are executed as root - you don't need sudo.

:)
So that means I can remove sudo from my script and remove the line I added in sudoers?
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

Re: How to : easy updates on a PC ?

#8 Post by f.r3d »

Thank you all very much for your help and advice!

EDIT: just so you know, my new script is:

Code: Select all

#!/bin/bash
apt-get update
apt-get upgrade -y
apt-get autoremove -y
apt-get clean
EDIT 2:
By default, anacron does not run while on battery power, so your laptop won’t get updates unless it is plugged in. You can change this by changing the value no to yes in /etc/default/anacron

Code: Select all

ANACRON_RUN_ON_BATTERY_POWER=yes
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

Re: [SOLVED] How to: easy updates on a PC ?

#9 Post by f.r3d »

Hi everyone, here is an update on the code that I use these days

Code: Select all

#!/bin/bash

# Define variables
EMAIL_ADDRESS="johndoe@email.com"
ERROR_LOG_FILE="/var/lib/automatic_updates_error.log"
COMPUTER_NAME="Computer XYZ"

# Load the error count from the error file
ERROR_COUNT=$(cat "$ERROR_LOG_FILE")

# Check if the computer is connected to the Internet
if ping -c 1 8.8.8.8 &> /dev/null; then

  # Update the system and Flatpak packages
  apt update && apt upgrade -y && flatpak update -y
  
  # Store the output of the update commands
  OUTPUT=$(apt update && apt upgrade -y && flatpak update -y)
  
  # Log the output to a file
  #echo "$OUTPUT" > /var/lib/automatic_updates_output.log

  # Check the result of the update
  if [ $? -eq 0 ]; then

    # If the update is successful, clean the apt cache and remove unnecessary packages
    apt-get autoremove -y && apt-get clean -y

    # Reset the error counter and update the error file
    ERROR_COUNT=0
    echo $ERROR_COUNT > "$ERROR_LOG_FILE"
  else
    # If the update is not successful, increment the error counter and update the error file
    ERROR_COUNT=$((ERROR_COUNT+1))
    echo $ERROR_COUNT > "$ERROR_LOG_FILE"

    # If the error counter has reached 2, send an email and reset the counter
    if [ $ERROR_COUNT -eq 2 ]; then
      mail -s "$COMPUTER_NAME: Update error" "$EMAIL_ADDRESS" <<< "System and Flatpak updates have failed two days in a row. \n\n$UPDATE_OUTPUT"
      ERROR_COUNT=0
      echo $ERROR_COUNT > "$ERROR_LOG_FILE"
    fi
  fi
else
  # If the computer is not connected to the Internet, exit with a return code of 1
  exit 1
fi
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

Re: [SOLVED] How to: easy updates on a PC ?

#10 Post by f.r3d »

By default anacron service is not started by systemd if the laptop is on battery mode. To change this behaviour, take a look at the instructions in :

Code: Select all

/usr/share/doc/anacron/README.Debian
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

bin
Posts: 55
Joined: 2019-03-07 12:02
Been thanked: 6 times

Re: [SOLVED] How to: easy updates on a PC ?

#11 Post by bin »

Although this is an old thread it is still relevant.
The lazy way I settled on to get round this is to configure unattended-upgrades as required and then once I'm happy it is working properly I start it with a cron job after boot.

Code: Select all

sudo crontab -e

Code: Select all

@boot sleep 60 && sudo apt update
@boot sleep 120 && sudo unattended-upgrades
Yes I know unattended-upgrades should pull in the update info I do it just in case there's a momentary glitch in getting to a repo.
This avoids messing with systemd timers and runs after every boot/reboot.
OK it's not the right way for a server or a machine that is seldom rebooted, but works for me.

User avatar
FreewheelinFrank
Global Moderator
Global Moderator
Posts: 2064
Joined: 2010-06-07 16:59
Has thanked: 38 times
Been thanked: 221 times

Re: [SOLVED] How to: easy updates on a PC ?

#12 Post by FreewheelinFrank »

Apt has its own timer these days:

Code: Select all

apt-daily.timer
https://wiki.debian.org/UnattendedUpgra ... systemd.29

User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

Re: [SOLVED] How to: easy updates on a PC ?

#13 Post by f.r3d »

A simplified version of the same script

Code: Select all

#!/bin/bash

# Define variables
EMAIL_ADDRESS="example@email.com"
COMPUTER_NAME="computer1"

# Check if the computer is connected to the Internet
if ping -c 1 8.8.8.8 &> /dev/null; then

  # Update the system and Flatpak packages
  if ! OUTPUT=$(apt update && apt upgrade -y && flatpak update -y 2>&1); then
    mail -s "$COMPUTER_NAME : Updates failed" "$EMAIL_ADDRESS" <<< "System and Flatpak updates have failed. \n\n $OUTPUT"
  else
    # If the update is successful, clean the apt cache and remove unnecessary packages
    apt-get autoremove -y && apt-get clean -y
  fi
else
  # If the computer is not connected to the Internet, exit with a return code of 1
  exit 1
fi
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

Re: [SOLVED] How to: easy updates on a PC ?

#14 Post by f.r3d »

Yet another simple version, except this time the line skip in the email actually works thanks to the echo command.

Code: Select all

#!/bin/bash

# Define variables
EMAIL_ADDRESS="your_email_address"
COMPUTER_NAME="computer_name"

# Check if the computer is connected to the Internet
if ping -c 1 8.8.8.8 &> /dev/null; then

  # Update the system and Flatpak packages
  OUTPUT=$(apt update && apt upgrade -y && flatpak update -y 2>&1)
  
  if [ $? -ne 0 ]; then
    echo -e "System and flatpak updates have failed. \n\n$OUTPUT" | mail -s "$COMPUTER_NAME : update error" "$EMAIL_ADDRESS"
  else
    # If the update is successful, clean the apt cache and remove unnecessary packages
    apt-get autoremove -y && apt-get clean -y
  fi
else
  # If the computer is not connected to the Internet, exit with a return code of 1
  exit 1
fi
I must add that in this script I use apt rather than apt-get because the logs are better presented for human eyes.
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

User avatar
fabien
Forum Helper
Forum Helper
Posts: 604
Joined: 2019-12-03 12:51
Location: Anarres (Toulouse, France actually)
Has thanked: 60 times
Been thanked: 141 times

Re: [SOLVED] How to: easy updates on a PC ?

#15 Post by fabien »

f.r3d wrote: 2023-05-29 12:02this time the line skip in the email actually works thanks to the echo command.
You can use $'\n' (outside the quotes) to avoid the subshell introduced by the pipe and the unsafe use of echo -e.

Code: Select all

mail -s "$COMPUTER_NAME : Updates failed" "$EMAIL_ADDRESS" <<<"System and Flatpak updates have failed."$'\n\n'"$OUTPUT"
Or using parameter expansion

Code: Select all

mail -s "$COMPUTER_NAME : Updates failed" "$EMAIL_ADDRESS" <<<"System and Flatpak updates have failed.${OUTPUT/#/$'\n\n'}"

Random_Troll
Posts: 444
Joined: 2023-02-07 13:35
Been thanked: 105 times

Re: How to : easy updates on a PC ?

#16 Post by Random_Troll »

f.r3d wrote: 2017-01-05 10:58Well I would rather use unattended-upgrades but I don't know how how to launch when the system starts.
The unattended-upgrades package supplies a systemd unit file that is enabled automatically and run at every boot (multi-user.target).

This works in concert with the apt-daily & apt-daily-upgrade unit files and their associated timer units to ensure upgrades should "just work" once the package is installed.

To configure the upgrades use

Code: Select all

# dpkg-reconfigure unattended-upgrades
Jeder nach seinen Fähigkeiten, jedem nach seinen Bedürfnissen.

User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

Re: [SOLVED] How to: easy updates on a PC ?

#17 Post by f.r3d »

So here I the new script where I took @fabien's advice into account.

Code: Select all

#!/bin/bash

# Define variables
EMAIL_ADDRESS="example@email.com"
COMPUTER_NAME="computer1"

# Check if the computer is connected to the Internet
if ping -c 1 8.8.8.8 &> /dev/null; then

  # Update the system and Flatpak packages
  if ! OUTPUT=$(apt update && apt upgrade -y && flatpak update -y 2>&1); then
    mail -s "$COMPUTER_NAME : Updates failed" "$EMAIL_ADDRESS" <<<"System and Flatpak updates have failed."$'\n\n' "$OUTPUT"
  else
    # If the update is successful, clean the apt cache and remove unnecessary packages
    apt-get autoremove -y && apt-get clean -y
  fi
else
  # If the computer is not connected to the Internet, exit with a return code of 1
  exit 1
fi
I will now take a closer look at unattended-upgrades and try to make it work so that it starts at every boot up (and not at a specific time) and to upgrade all packages, not just security updates. Thank you for that @Random_Troll.
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

User avatar
FreewheelinFrank
Global Moderator
Global Moderator
Posts: 2064
Joined: 2010-06-07 16:59
Has thanked: 38 times
Been thanked: 221 times

Re: [SOLVED] How to: easy updates on a PC ?

#18 Post by FreewheelinFrank »

f.r3d wrote: 2023-05-31 10:22 So here I the new script where I took @fabien's advice into account.

Code: Select all

#!/bin/bash

# Define variables
EMAIL_ADDRESS="example@email.com"
COMPUTER_NAME="computer1"

# Check if the computer is connected to the Internet
if ping -c 1 8.8.8.8 &> /dev/null; then

  # Update the system and Flatpak packages
  if ! OUTPUT=$(apt update && apt upgrade -y && flatpak update -y 2>&1); then
    mail -s "$COMPUTER_NAME : Updates failed" "$EMAIL_ADDRESS" <<<"System and Flatpak updates have failed."$'\n\n' "$OUTPUT"
  else
    # If the update is successful, clean the apt cache and remove unnecessary packages
    apt-get autoremove -y && apt-get clean -y
  fi
else
  # If the computer is not connected to the Internet, exit with a return code of 1
  exit 1
fi
I will now take a closer look at unattended-upgrades and try to make it work so that it starts at every boot up (and not at a specific time) and to upgrade all packages, not just security updates. Thank you for that @Random_Troll.
It's not just security updates anymore.

See:

viewtopic.php?f=19&t=141996&p=699902&hi ... es#p699899

apt-daily.timer by default starts at a randomised time around a fixed time, to avoid multiple computers hitting the server at the same time, but can be changed to run at boot, or 15-30 minutes after boot might be better.

User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

Re: [SOLVED] How to: easy updates on a PC ?

#19 Post by f.r3d »

I think I've set up unattended-upgrades correctly. This post was also very useful.
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

User avatar
f.r3d
Posts: 75
Joined: 2016-07-28 16:39
Location: France
Has thanked: 4 times

Re: [SOLVED] How to: easy updates on a PC ?

#20 Post by f.r3d »

FreewheelinFrank wrote: 2023-05-31 11:48
f.r3d wrote: 2023-05-31 10:22 So here I the new script where I took @fabien's advice into account.

Code: Select all

#!/bin/bash

# Define variables
EMAIL_ADDRESS="example@email.com"
COMPUTER_NAME="computer1"

# Check if the computer is connected to the Internet
if ping -c 1 8.8.8.8 &> /dev/null; then

  # Update the system and Flatpak packages
  if ! OUTPUT=$(apt update && apt upgrade -y && flatpak update -y 2>&1); then
    mail -s "$COMPUTER_NAME : Updates failed" "$EMAIL_ADDRESS" <<<"System and Flatpak updates have failed."$'\n\n' "$OUTPUT"
  else
    # If the update is successful, clean the apt cache and remove unnecessary packages
    apt-get autoremove -y && apt-get clean -y
  fi
else
  # If the computer is not connected to the Internet, exit with a return code of 1
  exit 1
fi
I will now take a closer look at unattended-upgrades and try to make it work so that it starts at every boot up (and not at a specific time) and to upgrade all packages, not just security updates. Thank you for that @Random_Troll.
It's not just security updates anymore.

See:

viewtopic.php?f=19&t=141996&p=699902&hi ... es#p699899

apt-daily.timer by default starts at a randomised time around a fixed time, to avoid multiple computers hitting the server at the same time, but can be changed to run at boot, or 15-30 minutes after boot might be better.
I've read that these timers are "persistent", meaning that when a trigger is missed (computer if off for example), it automatically launches at boot up. How do you set it up manually so that it starts at boot up?
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /

Post Reply