You are not logged in.

#1 2013-05-09 08:17:52

Mic92
Member
From: Germany
Registered: 2009-07-05
Posts: 21
Website

Fast Pacman Update Checker for bash/zsh

I saw some fancy notifiers using Desktop Notification or the systray on this forum, but nothing suitable for my arch server.
The following snippet check for updates once a day, whenever you spawn a new shell.

#!/bin/bash
# add this to your bashrc/zshrc
function _current_epoch {
  echo "$(($(date +%s) / 60 / 60 / 24))"
}

function _check_updates {
  (
    flock -n 9 || return # one concurrent update process at the time
    local ignored_pkgs="^linux"
    #local updates=`wc -l < /var/log/pacman-updates.log`
    local updates=`grep -Ev $ignored_pkgs /var/log/pacman-updates.log | wc -l`
    if [ $updates -gt 0 ]; then
      echo -n "There are $updates updates. Upgrade? (y/n) [n] "
      read line
      if [ "$line" = Y ] || [ "$line" = y ]; then
        yaourt -Syu --aur
        pacman -Qu | sudo tee /var/log/pacman-updates.log >/dev/null
      fi
    fi
    echo "$(_current_epoch)" > $HOME/.pacman-update
  ) 9> ~/.pacman-update.lck
}

if [[ $- == *i* ]] && # only interactive shells
  [ -e /var/log/pacman-updates.log ] && # only after first update
  [ ! -e /var/lib/pacman/db.lck ]; then # not if pacman is running
  if [ -e .pacman-update ]; then
    read last_epoch < $HOME/.pacman-update
    if [[ -n "$last_epoch" ]]; then
      if [ $(($(_current_epoch) - $last_epoch)) -ge 1 ]; then
        _check_updates
      fi
    fi
    unset last_epoch
  else
    _check_updates
  fi
fi

To keep my package cache up to date I use a systemd.timer unit.
It prefetchs all new packages without installing them or touching the database

#/etc/systemd/timer-daily.timer
[Unit]                                                                                                                                                
Description=Daily Timer                                                                                                                               
                                                                                                                                                      
[Timer]                                                                                                                                               
OnBootSec=10min                                                                                                                                       
OnUnitActiveSec=1d                                                                                                                                    
Unit=timer-daily.target                                                                                                                               
                                                                                                                                                      
[Install]                                                                                                                                             
WantedBy=basic.target
#/etc/systemd/timer-daily.target
[Unit]                                                                                                                                                
Description=Daily Timer Target                                                                                                                        
StopWhenUnneeded=yes 
#/etc/systemd/timer-daily.target.wants/pacman-update.service 
[Unit]
Description=Update pacman's package cache

[Service]
Type=oneshot
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
Environment=CHECKUPDATE_DB=/var/lib/pacman/checkupdate                                                                                                
ExecStartPre=/bin/sh -c "/usr/bin/checkupdates > /var/log/pacman-updates.log"                                                                         
ExecStart=/usr/bin/pacman --sync --upgrades --downloadonly --noconfirm --dbpath=/var/lib/pacman/checkupdate

Have fun.

UPDATE
Use the safer checkupdates script, so it will not touch the sync database, which prevents dangerous partial system upgrades

UPDATE 2
Do not check for updates if pacman is running. Use Type=oneshot to prevent timeout.

Last edited by Mic92 (2013-07-20 05:04:33)

Offline

#2 2013-05-09 08:48:57

Pierre
Developer
From: Bonn
Registered: 2004-07-05
Posts: 1,956
Website

Re: Fast Pacman Update Checker for bash/zsh

Running -Sy automatically is quite bad. Instead replace all this with the "checkupdates" command that already comes with pacman.

Offline

#3 2013-05-09 23:22:39

DaveCode
Member
Registered: 2008-08-15
Posts: 103

Re: Fast Pacman Update Checker for bash/zsh

Thank you Mic92, I appreciate this effort. Pacman needs more control options. Automatic update is an area for work though some may think otherwise. My experience is that logging pacnew files with nightly or weekly auto-reboot keeps auto-updates running fine. I inspect pacnew files by remote SSH once a month.

Pierre, what command line(s) did you mean by "checkupdates"?

Offline

#4 2013-05-09 23:51:51

Allan
Member
From: Brisbane, AU
Registered: 2007-06-09
Posts: 10,894
Website

Re: Fast Pacman Update Checker for bash/zsh

DaveCode wrote:

Pierre, what command line(s) did you mean by "checkupdates"?

Run "checkupdates"...   It is a script provided by pacman.

Offline

#5 2013-05-10 15:50:42

Mic92
Member
From: Germany
Registered: 2009-07-05
Posts: 21
Website

Re: Fast Pacman Update Checker for bash/zsh

Pierre wrote:

Running -Sy automatically is quite bad. Instead replace all this with the "checkupdates" command that already comes with pacman.

What are the possible consequence of just using pacman -Syuw.
As far a as I understand it only updates my local package database and download the packages?

Offline

#6 2013-05-10 16:20:17

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Fast Pacman Update Checker for bash/zsh

The problem is that it doesn't install the packages in question (but unsupervised - i.e. '--noconfirm' updating - is a bad idea too). If you now run 'pacman -S foo' without performing a proper full system update first, it can lead to breakage: https://bbs.archlinux.org/viewtopic.php?id=89328

Offline

#7 2013-05-11 11:20:45

Mic92
Member
From: Germany
Registered: 2009-07-05
Posts: 21
Website

Re: Fast Pacman Update Checker for bash/zsh

I replaced pacman -Sy with checkupdates. I assume it is save to update just the package cache without alternating the database, isn't it?

Offline

#8 2013-05-11 11:27:04

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Fast Pacman Update Checker for bash/zsh

Yes, checkupdates uses a temporary database.

Offline

#9 2013-05-14 08:35:37

DaveCode
Member
Registered: 2008-08-15
Posts: 103

Re: Fast Pacman Update Checker for bash/zsh

Tip, look into pacaur philosophy.

Shell helpers seem strange to mix with automation. My habit at shell is to enter pacaur -Syu and say no to the updates unless/until I want what I see, or the update list gets big. In that case I plan a restaurant break to coincide with my manual update.

Autoupdates can run at a particular time like early AM before working hours, when nightowls finally sleep but before morning office rush.

Offline

Board footer

Powered by FluxBB