Auto-Update Packages with apt in Cron
To refresh and upgrade packages from cron, run apt-get update and upgrade with the non-interactive flags. But for security patches, unattended-upgrades is the recommended tool.
The command
0 4 * * * apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y upgrade >> /var/log/apt-cron.log 2>&1
Run as root (root crontab or a system cron file). -y auto-confirms and DEBIAN_FRONTEND=noninteractive stops prompts from hanging the job.
Prefer unattended-upgrades for security
For just security patches, install unattended-upgrades instead — it's designed for this, handles reboots and package holds safely, and logs to /var/log/unattended-upgrades/. A blanket apt-get upgrade can pull in changes you didn't intend.
Clean up
Add apt-get -y autoremove && apt-get clean to reclaim space from old packages.
FAQ
How do I run apt update and upgrade from cron?
0 4 * * * apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y upgrade. Run it as root and log output to a file.
Why is my apt cron job hanging?
A prompt is waiting for input. Set DEBIAN_FRONTEND=noninteractive and pass -y so apt never asks interactively.
Should I use cron or unattended-upgrades for security patches?
Use unattended-upgrades. It is purpose-built for automatic security updates, handles holds and reboots, and is safer than a blanket apt-get upgrade in cron.