Automatically Update Arch Linux with Systemd


Automatically update Arch Linux installed packages if there is a new update available is important to keep the packages up-to-date and it will provide improvement of security fixes, stability, performance, features, and more.

 To keep the installed packages up-to-date, Arch Linux can automatically update if there is a new update available using a systemd service and timer (scheduler). It also helps to protect against zero-day attacks and vulnerabilities.

Information: The automatic update service below cannot automatically resolve package conflict during upgrade process, it will automatically skip or cancel the update process if conflict is detected. To resolve package conflict, run "pacman -Syu" (upgrade command) manually.

Create autoupdate service

Create a systemd service (autoupdate.service) using a text editor such as nano:
sudo nano /etc/systemd/system/autoupdate.service
Add following to the autoupdate.service:
[Unit]
 Description=Automatic Update
 After=network-online.target 

[Service]
 Type=simple
 ExecStart=/usr/bin/pacman -Syuq --noconfirm
 TimeoutStopSec=180
 KillMode=process
 KillSignal=SIGINT

[Install]
 WantedBy=multi-user.target
The autoupdate.service (autoupdate timer's dependency unit) require the network is up. On service stop systemd will send interrupt signal (SIGINT) then wait the process to stop, after 180 seconds (timeout) systemd.kill will terminate using SIGTERM signal. While installing a package, pacman will not immediately end the process after receive the interrupt signal (SIGINT), instead pacman will continue upgrading the package until finish, then quit the process. It will avoid the package to be broken or corrupted.

Create autoupdate timer

Create a systemd timer or scheduler (autoupdate.timer) using a text editor such as nano to run the automatic updates periodically (e.g. every 45 minutes):
sudo nano /etc/systemd/system/autoupdate.timer
Add following to the autoupdate.timer:
[Unit]
 Description=Automatic Update when booted up after 5 minutes then check the system for updates every 60 minutes

[Timer]
 OnBootSec=5min
 OnUnitActiveSec=60min
 Unit=autoupdate.service

[Install]
 WantedBy=multi-user.target
Enable the autoupdate.timer to automatically run at startup:
sudo systemctl enable /etc/systemd/system/autoupdate.timer

Troubleshooting

Package was broken and corrupted after interrupted install/update

If the automatic update were running when installing a package, but interrupted after power failure or system crash, then you receive an error when running pacman (e.g. error: target not found: package). Try to force re-install the broken or corrupted package then uninstall the package using following commands:
sudo pacman -S --force package && sudo pacman -R package
If you're encountered a conflicting file issue on upgrading process in example "gd: /directory/file/ exists in filesystem" you can try to force pacman to upgrade the package:
sudo pacman -Syu --force
Note: Use the "--force" command to force package to be removed or upgraded, but use carefully. If the above steps doesn't work you can try another method from ArchWiki pacman's guide

See also

  1. pacman - ArchWiki
  2. systemd - ArchWiki
  3. Image by dxiri / CC-BY 2.0

Comments

  1. It is always recommended to read the Arch news feed BEFORE doing an upgrade.
    There are important information e.g. changed behavior or known issues!!

    The way described above is not recommended and may making more problems then solving.

    ... but anyways.. I will try it ! LOL :D
    thx for sharing !

    ReplyDelete
  2. I "enhanced" the service to also update from AUR by using pacaur instead of just pacman:

    ExecStart=/usr/bin/pacaur -Syuq --noconfirm --needed --noprogressbar --noedit --silent

    pacaur updates both: your normal repos and AUR packages.

    I have also added the noconfirm, noedit etc options to be able to install for AUR packages automatically as well.

    btw: and I just want to mention that IgnorePkg is respected by pacaur as well.

    I will activate that all today on my laptop so thumbs pressed for the next months.. ;)

    ... and last but not least I modified it to not start after 5 min after boot. Instead I adjusted it to start after 8 hours which means this will be the last task of my work day ;)

    ReplyDelete
    Replies
    1. hmm... this doesn't work as pacaur refuses to run as root [which makes sense]

      Delete
  3. almost 2 years with this now. 3 issues so far all easily resolved by looking at arch update news at that time. this works so.. so well.

    ReplyDelete
  4. I currently created a package for this which also respects the Arch News for each package which needs to be upgraded plus AUR comments ;)

    I will publish the full guide when ready but until then I'm working on this (still a WIP!):

    https://github.com/steadfasterX/arch_uau-pkg

    ReplyDelete
    Replies
    1. nice. i woulse install this on every machine i manage

      Delete

Post a Comment