Automating System Updates on Fedora

Script without Logging

Keeping your system up to date is an important task for maintaining security and stability, but it can be time-consuming to manually run updates for each package manager. The standard software app for GNOME offers an automated system update feature, but it can be slow and unreliable. In this blog post, we’ll show you how to automate system updates on Fedora using a Bash script that updates the system with dnf, Flatpak, and Snap packages, and cleans up old packages and cache. We’ll also add logging to the script, so you can keep track of any errors or problems during the update process. Finally, we’ll show you how to adjust the permissions on the log file, so you can run the script as a regular user or as a cron job.

Automating system updates on Fedora can be done through a script that combines the use of dnf, flatpak, and snap. Here’s an example script that you can modify to suit your needs:

#!/bin/bash

# Update the system using dnf
dnf upgrade -y

# Update flatpak packages
flatpak update -y

# Update snap packages
snap refresh

# Clean up old packages and cache
dnf autoremove -y
dnf clean all
flatpak uninstall --unused -y
flatpak uninstall --unused --system -y
flatpak remove --unused -y
flatpak remove --unused --system -y
rm -rf /var/cache/flatpak/*
rm -rf /var/cache/snapd/*

Here’s a breakdown of what this script does:

  • dnf upgrade -y: updates the system using dnf and automatically confirms any prompts.
  • flatpak update -y: updates all installed Flatpak packages and automatically confirms any prompts.
  • snap refresh: updates all installed Snap packages.
  • dnf autoremove -y: removes any packages that are no longer needed on the system.
  • dnf clean all: removes cached packages from the system.
  • flatpak uninstall --unused -y: uninstalls all unused Flatpak packages for the current user.
  • flatpak uninstall --unused --system -y: uninstalls all unused Flatpak packages for the system.
  • flatpak remove --unused -y: removes all unused Flatpak runtimes for the current user.
  • flatpak remove --unused --system -y: removes all unused Flatpak runtimes for the system.
  • rm -rf /var/cache/flatpak/*: removes all cached Flatpak packages from the system.
  • rm -rf /var/cache/snapd/*: removes all cached Snap packages from the system.

Automate the script

To automate the script, you can create a cron job that runs it at a specified interval. Here’s an example of how to set up a cron job to run the script every day at 4 AM:

  1. Open the terminal and run the following command:
crontab -e
  1. Add the following line to the end of the file:
0 4 * * * /path/to/script.sh

Note: Replace /path/to/script.sh with the actual path to your script.

  1. Save the file and exit.

This will schedule the script to run at 4 AM every day, updating your system and cleaning up any unnecessary packages and cache files.

Script with logging

If you like to have logging here is an updated version of the script with logging and timestamps:

#!/bin/bash

# Set the log file location and create it if it doesn't exist
LOG_FILE=/var/log/system_update.log
if [ ! -f "$LOG_FILE" ]; then
  touch "$LOG_FILE"
fi

# Log the start time
echo "Starting system update at $(date +%Y-%m-%d\ %H:%M:%S)" >> "$LOG_FILE"

# Update the system using dnf
if ! dnf upgrade -y >> "$LOG_FILE" 2>&1; then
  echo "Error updating the system with dnf" >> "$LOG_FILE"
fi

# Update flatpak packages
if ! flatpak update -y >> "$LOG_FILE" 2>&1; then
  echo "Error updating Flatpak packages" >> "$LOG_FILE"
fi

# Update snap packages
if ! snap refresh >> "$LOG_FILE" 2>&1; then
  echo "Error updating Snap packages" >> "$LOG_FILE"
fi

# Clean up old packages and cache
if ! dnf autoremove -y >> "$LOG_FILE" 2>&1; then
  echo "Error removing old packages with dnf" >> "$LOG_FILE"
fi
if ! dnf clean all >> "$LOG_FILE" 2>&1; then
  echo "Error cleaning dnf cache" >> "$LOG_FILE"
fi
if ! flatpak uninstall --unused -y >> "$LOG_FILE" 2>&1; then
  echo "Error uninstalling unused Flatpak packages for current user" >> "$LOG_FILE"
fi
if ! flatpak uninstall --unused --system -y >> "$LOG_FILE" 2>&1; then
  echo "Error uninstalling unused Flatpak packages for the system" >> "$LOG_FILE"
fi
if ! flatpak remove --unused -y >> "$LOG_FILE" 2>&1; then
  echo "Error removing unused Flatpak runtimes for current user" >> "$LOG_FILE"
fi
if ! flatpak remove --unused --system -y >> "$LOG_FILE" 2>&1; then
  echo "Error removing unused Flatpak runtimes for the system" >> "$LOG_FILE"
fi
if ! rm -rf /var/cache/flatpak/* >> "$LOG_FILE" 2>&1; then
  echo "Error removing cached Flatpak packages" >> "$LOG_FILE"
fi
if ! rm -rf /var/cache/snapd/* >> "$LOG_FILE" 2>&1; then
  echo "Error removing cached Snap packages" >> "$LOG_FILE"
fi

# Log the end time
echo "System update finished at $(date +%Y-%m-%d\ %H:%M:%S)" >> "$LOG_FILE"

This version of the script uses the date command to add timestamps to the log messages, and redirect the output of each command to the log file using >> "$LOG_FILE" 2>&1. This way, any errors or problems that occur during the update process will be logged in the file. The log file location is set to /var/log/system_update.log, but you can change this to any location that you prefer.

Note that if you are running this script as a cron job, you may need to adjust the permissions on the log file to allow the cron job to write to it.

To adjust the permissions of the log file, you can use the chmod command. For example, if you want to allow the owner and group to read and write to the file, and everyone else to have no permissions, you can run the following command:

chmod 660 /var/log/system_update.log

This will set the file permissions to -rw-rw----, which means the owner and group can read and write to the file, and everyone else has no permissions. You may need to run this command as root or using sudo, depending on your system configuration.

Leave a Comment

Your email address will not be published. Required fields are marked *