I created a script to automatically update my Ubuntu Server machines every day at 1am using crontab. This script allows my machines to automatically update during the night without having to manually do anything or have my homelab interrupted during the day.

This is my update script:

# !/bin/sh

## Script to Update Server
clear
    # Create Host Variable for Machine
    host=$(cat /etc/hostname)
    echo "$host"
clear
## NTFY Notification Start
curl -H "Title: $host Update" -H "Markdown: yes" -d "$host Update Initiated" https://ntfy.ahillier.dev/"$host"

    # Update package lists
    sudo NEEDRESTART_MODE=a apt update -y

    # Upgrade installed packages
    sudo NEEDRESTART_MODE=a apt upgrade -y

    # Upgrade the distribution (including the OS)
    sudo NEEDRESTART_MODE=a apt dist-upgrade -y

    # Remove unnecessary files
    sudo NEEDRESTART_MODE=a apt autoremove -y
    sudo NEEDRESTART_MODE=a apt clean -y

## NTFY Notification End
curl -H "Title: $host Update" -H "Markdown: yes" -d "$host Update Complete" https://ntfy.ahillier.dev/"$host"

# Intellectual Property
echo "Script By: "
echo "
 █████   █████ ███ ████ ████ █████      ███ ████ ████
░░███   ░░███ ░░░ ░░███░░███░░███      ░░░ ░░███░░███
 ░███    ░███ ████ ░███ ░███ ░███████  ████ ░███ ░███ █████ ████ ██████  ████████
 ░███████████░░███ ░███ ░███ ░███░░███░░███ ░███ ░███░░███ ░███ ███░░███░░███░░███
 ░███░░░░░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███░███████  ░███ ░░░
 ░███    ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███░███░░░   ░███
 █████   ████████████████████████████  ███████████████░░███████░░██████  █████
░░░░░   ░░░░░░░░░░░░░░░░░░░░░░░░░░░░  ░░░░░░░░░░░░░░░  ░░░░░███ ░░░░░░  ░░░░░
                                                       ███ ░███
                                                      ░░██████
                                                       ░░░░░░
"
echo "https://hillbillyer.net"
echo "contact@hillbillyer.net"

sleep 3s
sudo reboot

This is the line I then added inside my Ubuntu Server using sudo crontab -e:

0 1 * * * /hill/update.sh