Quick Start (5 min)
Standard cron is a sysadmin staple, but it fails in two specific scenarios: one-time tasks and systems that aren’t online 24/7. If your machine is powered down when a cron job is scheduled, that task simply evaporates. at and anacron bridge these gaps perfectly.
First, ensure at is installed on your system. On Ubuntu or Debian, run:
sudo apt update && sudo apt install at
sudo systemctl enable --now atd
Want to run a backup at 2:00 AM tonight? Instead of wrestling with a complex crontab file, just pipe the command directly to at:
echo "/home/user/scripts/backup.sh" | at 02:00
Anacron handles periodic tasks differently. It tracks intervals rather than wall-clock time. If your laptop stays closed during a scheduled weekly cleanup, anacron triggers it 15 minutes after you flip the lid back up. You can inspect its configuration by running cat /etc/anacrontab.
Deep Dive into Linux Task Scheduling
The Precision of the ‘at’ Command
I use at constantly for “fire and forget” tasks. It is ideal for rebooting a server during a 15-minute maintenance window or clearing out a 50GB temporary directory after a data migration. The syntax is remarkably versatile. You can specify absolute times (at 14:30), relative offsets (at now + 2 hours), or specific dates like at 10:00 AM July 25.
Every submitted job receives a unique ID. Manage these using atq to list pending tasks and atrm to cancel them. One hard lesson: at snapshots your current environment variables at the moment of submission. Even so, always use absolute paths for scripts and binaries to prevent path-related failures.
Solving the ‘Off-time’ Problem with anacron
Anacron is essential for workstations and local dev boxes. While a cloud VPS might stay up for 300 days straight, your laptop likely goes to sleep every night. Anacron ensures your midnight maintenance jobs actually execute by tracking timestamps in /var/spool/anacron.
The structure of /etc/anacrontab looks like this:
# period in days | delay in minutes | job-identifier | command
1 5 daily-backup /usr/local/bin/backup.sh
7 15 weekly-cleanup /usr/local/bin/cleanup.sh
In this setup, the daily backup runs once every 24 hours. If the system was off, anacron waits 5 minutes after startup before launching the script. This staggered start prevents a dozen maintenance tasks from choking your CPU the second you log in.
On a recent project with a 4GB RAM Ubuntu server, I moved several heavy data-processing tasks to at jobs scheduled for 3:00 AM. This reduced peak CPU load from 90% to under 20% during business hours. The system stayed snappy for users, and I didn’t have to manage a permanent crontab for temporary needs.
Advanced Usage and Tricks
Utilizing the ‘batch’ Command
The batch command is a powerful, often overlooked tool in the at package. It functions exactly like at, but with a brain: it only runs when the system load average drops below 1.5. This is perfect for heavy lifting like video encoding or compiling large kernels without impacting your current work.
batch <<EOF
/usr/bin/process-heavy-data.sh
EOF
Restricting Access
Security is vital in multi-user environments. Linux uses /etc/at.allow and /etc/at.deny to gate access. If at.allow exists, only users listed there can schedule jobs. I recommend a whitelist approach on production servers to prevent junior developers from accidentally exhausting system resources with recursive scripts.
Handling Output and Errors
By default, at tries to email stdout and stderr to your local user account. Since local system mail is rarely checked, I always redirect output to a dedicated log file to keep troubleshooting simple:
echo "/path/to/script.sh >> /var/log/myscript.log 2>&1" | at now + 1 hour
Practical Tips for Reliable Scheduling
- Absolute paths are mandatory: Don’t just run
python script.py. Use/usr/bin/python3 /home/user/scripts/script.py. This prevents 90% of scheduling failures. - Manually test before you automate: Run your script in a standard shell first. If it requires interactive input (like a password prompt), it will fail silently in the background.
- Logs tell the full story: If a periodic task vanishes, check
journalctl -u anacron. You can also force a test run of all jobs withsudo anacron -f. - Keep track of Job IDs: When scheduling multiple
attasks, note the IDs. It’s much faster toatrm 42than to scramble when you realize a script has a bug. - Sync your system clock:
atis only as accurate as your clock. Ensure NTP is active so your 3:00 AM maintenance doesn’t accidentally run during the 9:00 AM peak.
Mastering at and anacron gives you the flexibility that standard cron lacks. Whether you’re managing a local workstation or a high-traffic server, these tools ensure your tasks run exactly when they should—even if the power was out.

