Help with my simple update-maintenance script

Hello there

I made a simple script to:

  • refresh mirrors
  • update packages from all sources including AUR, flatpaks and snaps
  • remove orphaned packages
  • clean package cache
  • refresh keys

i try to avoid multiple sudo inputs ,any thoughts?

also i am pretty new to linux and bash scripting so dont hurt me much :stuck_out_tongue:

#!/bin/bash

echo Refresh Mirrors?
echo " "
read mirrors
if [[ "$mirrors" = "y" ]]; then
echo " "
echo " "
sudo pacman-mirrors --continent --api --protocols all --set-branch stable
wait
echo " "
echo " "
sudo pacman -Syyu
echo " "
echo " "
pamac upgrade -a
echo " "
echo " "
sudo flatpak update -y & snap refresh
echo " "
echo " "
pamac remove -o
echo " "
echo " "
pamac clean
echo " "
echo " "

else
sudo pacman -Syyu
echo " "
echo " "
pamac upgrade -a
echo " "
echo " "
sudo flatpak update -y & snap refresh
echo " "
echo " "
pamac remove -o
echo " "
echo " "
pamac clean
echo " "
echo " "
fi

echo Refresh Keys?
echo " "
read keys
if [[ "$keys" = "y" ]]; then
echo " "
echo " "
sudo pacman-key --refresh-keys
echo " "
echo " "
sudo pacman-key --populate archlinux manjaro
echo " "
echo " "
else
$SHELL
fi


$SHELL

sudo does remember the authentication for a number of minutes ─ I think it’s five minutes by default ─ so two subsequent sudo calls should only require your password once, provided that the first call finishes within the allotted time.

But I don’t understand why you’ve included a wait after the first sudo. You can simply use a condition instead, like so… :arrow_down:

sudo pacman-mirrors [and all that jazz] && sudo pacman -Syyu

The && is a logical AND operand. So the update process will only run once the mirror list has been updated, and will not run if the mirror list update fails for whatever reason.

That said, instead of sudo pacman -Syyu, you can also use pamac update, which doesn’t require sudo. But it’ll still ask you for a password, of course.

And speaking of which, if you specifically want to update AUR packages with the pamac command, then it might be wise to also add --devel, just in case some of your AUR packages are -git packages.

Also, you don’t need to put a quoted space after echo. An echo without parameters will simply print a blank line.

The above all said, I would do it like this… :arrow_down:

#!/bin/bash

read -n1 -p "Refresh Mirrors? [y/N] "
case ${REPLY} in
       "Y" | "y" )
          sudo pacman-mirrors --continent --api --protocols all --set-branch stable
          echo
          echo
          ;;
esac
pamac update && pamac update --aur --devel && pamac remove -o && pacmac clean
sudo flatpak update -y && snap refresh
echo
echo
unset REPLY
read -n1 -p "Refresh Keys? [y/N] "
case ${REPLY} in
       "Y" | "y" )
          sudo pacman-key --refresh-keys && sudo pacman-key --populate archlinux manjaro
          echo
          echo
          ;;
esac
unset REPLY
return

I don’t know why you’re trying to invoke the shell there. If your intent is to get a simple interactive shell, then just let the script execute, and it’ll return to the command prompt after running.

The return command that I’ve added also isn’t necessary, but it allows for the script to be called from within another script or from within a subroutine. It’s just scripting hygiene. :wink:

3 Likes

thanks for your answer , i ll study your code
my main problem is that pamac requires gui authentication outside shell, is there any way to bypass this?

No, it does require authentication, but not necessarily by way of the GUI. It is configured to use an authentication GUI when running from a terminal window or a GUI environment, but when run from a tty, it’ll simply prompt you for the password with a text prompt. :wink:

1 Like

Something to think about

  • There may be a pamac clean service/timer running on your behalf automatically already. By default I believe it keeps 3 versions (/etc/pamac.conf) of a package in cache (/var/cache/pacman/pkg/), and runs the first Saturday of a month at 15:00.
   # List status of the service and timer
    ===> systemctl status pamac-cleancache.{service,timer}

    # List all timers
    ===> systemctl list-timers  
  • Might want to incorporate a pacdiff at the end of an upgrade to look for any .pacnew files. There should be some message on the console or log (/var/log/pacman.log) about a .pacnew file being created.

  • A reminder to read the Stable Announcement forum (at least the first 2 entries) for the upgrade.

  • If you are new to bash scripting and/or command line you might find LinuxCommand.org: Learn The Linux Command Line. Write Shell Scripts., helpful.

Questioning

  1. I believe a user needs to run pacman-mirrors -f instead or in addition to --continent. The man page says country and fasttrack are mutually exclusive. One option builds the custom mirrorlist and the other builds the runtime mirrorlist (/etc/pacman.d/mirrorlist) based on some “speed” tests.

  2. I don’t think keys need to be updated separately unless there is a problem. It is usually done as part of the upgrade, and at the beginning. I could be wrong, but I don’t think this is necessary.

1 Like

thank you stargazer

i am aware of clean timer however i didn’t knew about these pac files

– continent option sorts mirrors based on speed so i am ok i believe

i made the script as a all in one upgrade and maintenance solution ,so i included keys refreshment just in case of need

Maybe you dont want to be prompted to dive into pacnews every update … but you could get a quick report/reminder for them:

pacdiff -o

That will just print existing *pac files, so you can choose to look at and handle them as you wish.

Also gonna leave this here: System Maintenance - Manjaro

1 Like

thanks cscs

i ll implement your suggestion to my script (every few monts) as a reminder using arch wiki find suggestion

find /etc -regextype posix-extended -regex “.+.pac(new|save)” 2> /dev/null

https://wiki.archlinux.org/title/Pacman/Pacnew_and_Pacsave

This fails on my system. (be careful about what type of quotes you are using)
This works:

find /etc -regextype posix-extended -regex ".+\.pac(new|save)" 2> /dev/null

Any reason you are trying to find them manually, and only in the /etc/ directory?

PS … as a quick test I measured the time to do the ‘find’ version on all of root, as well as for pacdiff.

find = 26.515s
pacdiff = 0.785s
1 Like

pacdiff returns fewer results though

Maybe from neglected pac* files related to packages that are no longer installed?
pacman -Qo /path/to/file for any of the ones pacdiff missed to see.
(omitting the ‘save/new’ part)

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.