Update Ubuntu using Apt & Cron

There are several methods to update Ubuntu. These methods include package updates via the desktop, the unattended upgrade script, and good old Apt. As the title suggests, this post explores the last option using Cron.

Using the Apt command via Cron is mainly intended if you have installed a cut-down, customised or server version of Ubuntu. Conversely, Desktop users using the full install do not need to worry since the GUI automatically handles updates.

You should use the unattended upgrade script if you want to write the details and changes to the log files and send a mail report to a system admin user. However, this can be scripted using Bash if you wish.

The unattended-upgrades script in Ubuntu is handy when you want to:

  • Do a dry run of an upgrade without actually installing anything.
  • Handle security and non-security updates separately.

Therefore, this post is mainly intended for Ubuntu server users with a lightweight install. Thus using Apt and Cron would be a perfect fit and a no-frills approach.

As a matter of caution – upgrades can potentially break things—equally; it’s safer to be up-to-date.

Let’s get started!

First, open Cron using the following command:

sudo crontab -e

Note that you need to run this command using sudo. Alternatively, you need to be logged in as root.

Next, add the following lines using your favourite text editor and save:

0 17 * * 7 /usr/bin/apt update
0 18 * * 7 /usr/bin/apt upgrade -q -y
0 19 * * 7 /usr/bin/apt autoclean

The Apt update command only gets the information about the latest version of packages available for your system. While the Apt upgrade command on Ubuntu downloads and upgrades the package to the new version.

Autoclean clears the local repository of retrieved package files. But it only removes files that can no longer be downloaded and are effectively useless.

You can set your Cron to run the Ubuntu APT updates anytime. The example above runs the upgrade on Sundays starting at 17:00, then at 18:00 and finally at 19:00.

Using Ansible?

Ansible allows for easy automation. For example, you can add the following to your playbook to update Ubuntu using Apt and Cron:

tasks:
    #
    # Add update cron
    #
    - name: Update
      become: yes
      become_method: sudo
      cron:
        name: "update"
        user: "root"
        weekday: "7"
        minute: "0"
        hour: "19"
        job: "/usr/bin/apt update -q -y"
        state: present

    #
    # Add upgrade cron
    #
    - name: Upgrade
      become: yes
      become_method: sudo
      cron:
        name: "upgrade"
        user: "root"
        weekday: "7"
        minute: "0"
        hour: "20"
        job: "/usr/bin/apt upgrade -q -y"
        state: present

    #
    # Add autoclean cron
    #
    - name: Autoclean
      become: yes
      become_method: sudo
      cron:
        name: "autoclean"
        user: "root"
        weekday: "7"
        minute: "0"
        hour: "23"
        job: "/usr/bin/apt autoclean"
        state: present

Wrapping Up

You have learned the differences between Apt updates using Cron, the desktop GUI update method, and Ubuntu’s unattended-upgrades script. In addition, you learned how to update, upgrade and auto-clean your Apt cache using Cron in Ubuntu.

You May Also Be Interested In

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.