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 ?
[SOLVED] How to: easy updates on a PC ?
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!
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 /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
- alan stone
- Posts: 269
- Joined: 2011-10-22 14:08
- Location: In my body.
Re: How to : easy updates on a PC ?
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.
> 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.
Re: How to : easy updates on a PC ?
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.
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.
Re: How to : easy updates on a PC ?
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:
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?
Code: Select all
#!/bin/bash
sudo apt-get update
sudo apt-get upgrade -y
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 /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
Re: How to : easy updates on a PC ?
So I decided to use anacron.
I created a script in /etc/cron.daily/automatic_updates
then I used
I edited /etc/anacrontab and added the line
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:
an then I added
Is it correct ?
I created a script in /etc/cron.daily/automatic_updates
Code: Select all
#!/bin/bash
sudo apt-get update
sudo apt-get upgrade -y
Code: Select all
sudo chmod 755 /etc/cron.daily/automatic_updates
Code: Select all
@daily 5 automatic_updates /etc/cron.daily/automatic_updates
So in order to enable the system to run my bash script without asking me for password, I did:
Code: Select all
sudo visudo
Code: Select all
[myusername] ALL=(ALL) NOPASSWD: /etc/cron.daily/automatic_updates
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 /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
- alan stone
- Posts: 269
- Joined: 2011-10-22 14:08
- Location: In my body.
Re: How to : easy updates on a PC ?
So that means I can remove sudo from my script and remove the line I added in sudoers?wizard10000 wrote:anacron jobs are executed as root - you don't need sudo.
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
Re: How to : easy updates on a PC ?
Thank you all very much for your help and advice!
EDIT: just so you know, my new script is:
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
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
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 /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
Re: [SOLVED] How to: easy updates on a PC ?
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 /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
Re: [SOLVED] How to: easy updates on a PC ?
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 /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
Re: [SOLVED] How to: easy updates on a PC ?
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.
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.
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
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.
- FreewheelinFrank
- Global Moderator
- Posts: 2082
- Joined: 2010-06-07 16:59
- Has thanked: 38 times
- Been thanked: 225 times
Re: [SOLVED] How to: easy updates on a PC ?
Apt has its own timer these days:
https://wiki.debian.org/UnattendedUpgra ... systemd.29
Code: Select all
apt-daily.timer
Re: [SOLVED] How to: easy updates on a PC ?
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 /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
Re: [SOLVED] How to: easy updates on a PC ?
Yet another simple version, except this time the line skip in the email actually works thanks to the echo command.
I must add that in this script I use apt rather than apt-get because the logs are better presented for human eyes.
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
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
- fabien
- Forum Helper
- Posts: 624
- Joined: 2019-12-03 12:51
- Location: Anarres (Toulouse, France actually)
- Has thanked: 60 times
- Been thanked: 146 times
Re: [SOLVED] How to: easy updates on a PC ?
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"
Code: Select all
mail -s "$COMPUTER_NAME : Updates failed" "$EMAIL_ADDRESS" <<<"System and Flatpak updates have failed.${OUTPUT/#/$'\n\n'}"
-
- Posts: 444
- Joined: 2023-02-07 13:35
- Been thanked: 105 times
Re: How to : easy updates on a PC ?
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.
Re: [SOLVED] How to: easy updates on a PC ?
So here I the new script where I took @fabien's advice into account.
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.
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 /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
- FreewheelinFrank
- Global Moderator
- Posts: 2082
- Joined: 2010-06-07 16:59
- Has thanked: 38 times
- Been thanked: 225 times
Re: [SOLVED] How to: easy updates on a PC ?
It's not just security updates anymore.f.r3d wrote: ↑2023-05-31 10:22 So here I the new script where I took @fabien's advice into account.
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.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
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.
Re: [SOLVED] How to: easy updates on a PC ?
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 /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
Re: [SOLVED] How to: easy updates on a PC ?
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?FreewheelinFrank wrote: ↑2023-05-31 11:48It's not just security updates anymore.f.r3d wrote: ↑2023-05-31 10:22 So here I the new script where I took @fabien's advice into account.
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.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
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.
Debian 11 Gnome 64bit
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /
Thinkpad T460
4X Intel Core i7-6600U / 8GB Ram / Intel HD Graphics 520 / 256GB SSD /