Systemd provides a powerful way to schedule recurring maintenance tasks without relying on cron.
By creating a simple service and timer, you can automatically update your system every night — fully noninteractively — with logs stored in journalctl.
This method is ideal for servers, VPS environments, and long-running workstations that need consistent, unattended updates.
1. Create the Upgrade Script
First, create a reusable script at /usr/local/sbin/auto-upgrade.sh:
sudo tee /usr/local/sbin/auto-upgrade.sh > /dev/null <<'EOF'
#!/bin/bash
set -e
LOGFILE="/var/log/auto-upgrade.log"
{
echo "=== Auto-upgrade started: $(date) ==="
env DEBIAN_FRONTEND=noninteractive APT_LISTCHANGES_FRONTEND=none apt-get update -y
env DEBIAN_FRONTEND=noninteractive APT_LISTCHANGES_FRONTEND=none apt-get \
-o Dpkg::Options::="--force-confdef" \
-o Dpkg::Options::="--force-confold" -y dist-upgrade
apt-get -y autoremove
apt-get -y autoclean
echo "=== Auto-upgrade finished: $(date) ==="
} >> "$LOGFILE" 2>&1
EOF
sudo chmod +x /usr/local/sbin/auto-upgrade.shThis script:
- Updates all packages noninteractively.
- Keeps existing configuration files.
- Logs all actions to /var/log/auto-upgrade.log.
2. Create the Systemd Service
Save this file as /etc/systemd/system/auto-upgrade.service:
[Unit] Description=Automatic Ubuntu 24.04 system upgrade After=network-online.target Wants=network-online.target [Service] Type=oneshot ExecStart=/usr/local/sbin/auto-upgrade.sh Nice=10 IOSchedulingClass=idle StandardOutput=journal StandardError=journal [Install] WantedBy=multi-user.target
Explanation:
- Runs only once per trigger (Type=oneshot).
- Defers until the network is available.
- Runs with lower CPU and I/O priority to minimize system impact.
- Outputs to journalctl for easy inspection (journalctl -u auto-upgrade.service).
3. Add the Systemd Timer
Create /etc/systemd/system/auto-upgrade.timer:
[Unit] Description=Run automatic Ubuntu 24.04 upgrade daily [Timer] OnCalendar=03:00 Persistent=true RandomizedDelaySec=300 [Install] WantedBy=timers.target
What it does:
- Triggers the upgrade daily at 3:00 AM.
- Adds a random delay of up to 5 minutes to prevent simultaneous updates on clusters.
- Persistent=true ensures missed runs are executed once the system is back online.
4. Enable and Start the Timer
Run the following commands:
sudo systemctl daemon-reload sudo systemctl enable --now auto-upgrade.timer
You can check its status anytime:
systemctl status auto-upgrade.timer
And view logs for recent runs:
journalctl -u auto-upgrade.service -n 50
5. (Optional) Auto-Reboot After Kernel Upgrades
If you want automatic reboots after updates that require them, enable it:
sudo apt-get install -y unattended-upgrades sudo sed -i 's|//Unattended-Upgrade::Automatic-Reboot "false";|\ Unattended-Upgrade::Automatic-Reboot "true";|' /etc/apt/apt.conf.d/50unattended-upgrades
Conclusion
With this setup, your Ubuntu 24.04 system will:
- Automatically fetch and apply updates daily.
- Log all actions safely.
- Run efficiently during low-load hours.
It’s a clean, modern replacement for cron — and it works perfectly for both servers and desktops.