You are not logged in.

#1 2019-07-01 09:16:55

vda
Member
From: Italy
Registered: 2015-04-10
Posts: 19

[Soved] Automatic system update

Hi all,

I'm writing a little script to automatically update system using pacman.

The code is quite simple and the script seems to work well (I also prefer to reboot system after any update).

$ cat /opt/System/system_update 
#!/bin/bash

#
# Description:	it update system automatically 
#
# Usage:	system_update
#
# Example:	./system_update
#
# Author:	VDA
#
# Last update: 2019.06.25
#

[ $EUID -ne 0 ] && { echo "$0 - Error: you must be root to run this script."; exit 1; } 

read -p "Attention please: your system will be updated and restarted. Changes cannot be canceled. Do you want to continue (Y/n)? " q
[ $q != "Y" ] && exit 1

pacman -Syy && pacman -Syu --noconfirm && reboot

exit 0

I have just a question: how can I check if no updates are available (unfortunately, pacman -S returns 0 even when there are no updates available)? In that case, I could avoid updating the system...

Thanks in advance,

VDA

Last edited by vda (2019-07-01 14:48:35)


I'm sorry for my english: I'm still learning...

Offline

#2 2019-07-01 09:31:26

Lone_Wolf
Member
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 11,868

Re: [Soved] Automatic system update

Bad idea, archlinux / pacman expect a human to make some decisions.
You'd have to check pacman log after every "automatic update" for problems.

An alert there are updates is useful, the checkupdates script can tell you if there are updates without running pacman .

https://wiki.archlinux.org/index.php/Sy … nsupported


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.


(A works at time B)  && (time C > time B ) ≠  (A works at time C)

Offline

#3 2019-07-01 10:46:10

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,933
Website

Re: [Soved] Automatic system update

Multiple concerns.
1) See @Lone_Wolf
2) User-defined scripts should go into ~/.local/bin or /usr/local/bin if they are intended for all users on the system, not /opt.
3)

vda wrote:
[ $EUID -ne 0 ] && { echo "$0 - Error: you must be root to run this script."; exit 1; } 

Pacman will fail by itself if a transaction that requires root privileges is executed by a non-root user.
4) 

vda wrote:
read -p "Attention please: your system will be updated and restarted. Changes cannot be canceled. Do you want to continue (Y/n)? " q
[ $q != "Y" ] && exit 1

Since "Y" is capitalized, I'd expect it to continue when I just hit enter as this usually means "this is the default if you do not select something else".
5)

vda wrote:
pacman -Syy && pacman -Syu --noconfirm && reboot

The -Syy is unnecessary.

pacman -Syu

does all you need. Also a blind reboot at the end could break the system if e.g. an initramfs could not be generated due to lack of disk space etc.
6)

vda wrote:

I have just a question: how can I check if no updates are available (unfortunately, pacman -S returns 0 even when there are no updates available)? In that case, I could avoid updating the system...

You could check its output instead of the return value.

Offline

#4 2019-07-01 11:36:46

woodape
Member
Registered: 2015-03-25
Posts: 159

Re: [Soved] Automatic system update

Someone more informed can tell me if this is a bad idea, but I have

*/20 * * * * pacman -Syuw

as a cronjob. Downloads updated packages every 20 minutes. I still have to do pacman -Syu to actually do the upgrade, but I don't actively wait for packages to download.

Offline

#5 2019-07-01 11:47:32

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,933
Website

Re: [Soved] Automatic system update

@woodape
Opinions differ on this question.
I personally use the -Syuw command myself (not via cron but via systemd.{service,timer} pair) on systems with low internet speed, so that I can let them download the updates over night and later install them manually.
I have not yet run into problems with this course of action.

Offline

#6 2019-07-01 11:57:03

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,452
Website

Re: [Soved] Automatic system update

I'll offer the other opinion.  That is a bad idea as it sets you up for a partial upgrade.  As long as you *never* install a package with `pacman -S $pkgname` you could avoid the fallout from that, but given that there are far better ways to achieve the same goal, I'd say just don't dance along such a cliff in the first place.

Instead of this, just use a temporary database path (like checkupdates does) and download all available package updates, then all the newest .pkg.tar.xz files are on your system, but you have not set yourself up for a partial upgrade.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Online

#7 2019-07-01 12:01:25

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,788
Website

Re: [Soved] Automatic system update

"-Syuw" has the same problem as just running -Sy. If you then perform a "-S <package>", you will end up with a partially updated system.

The next version of checkupdates will have an additional flag to download available updates safely (i.e. without touching your system package databases), but until that is released, you can just use the checkupdates db manually (e.g. '--dbpath /tmp/checkup-db-$USER').


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#8 2019-07-01 14:47:48

vda
Member
From: Italy
Registered: 2015-04-10
Posts: 19

Re: [Soved] Automatic system update

Thank you so much guys. You're the best wiki I could wish for.

VDA



EDIT

here my final solution:

#!/bin/bash

#
# Description:  automatic system update
#
# Usage:        system_update
#
# Example:      ./system_update
#
# Author:       VDA
#
# Last update: 2019.07.03
#

[ $EUID -ne 0 ] && { echo "$0 - Error: you must be root to run this script."; exit 1; }

read -p "Attention please: your system will be updated and restarted. Changes cannot be canceled. Do you want to continue (Y/n)? " q
[[ $q != "Y" ]] && exit 1

sudo pacman -Syy
[ $(sudo pacman -Su --print | wc -c) == 0 ] && { echo "No updates avaible"; exit 1; }

pacman -Syy && pacman -Su --noconfirm && reboot

exit 0

Thanks again.

Last edited by vda (2019-07-04 16:00:53)


I'm sorry for my english: I'm still learning...

Offline

Board footer

Powered by FluxBB