Scheduling & Automation in Linux: Mastering Cron, At, and Systemd Timers
One of the most powerful features of Linux is the ability to automate tasks. Whether it's running a backup every night, sending reports every week, or cleaning up logs at boot, scheduling saves time and prevents human error.
Linux offers multiple ways to schedule tasks:
at→ run a job once at a specific timecron→ run jobs repeatedly on a scheduleanacron→ run cron-like jobs that may have been missed (good for laptops)systemd timers → the modern replacement/enhancement to cron
This guide covers all four, with examples, cheatsheets, and practice exercises.
One-Time Jobs with at
The at command schedules a job to run once in the future.
Enable the service (CentOS/WSL2)
sudo systemctl start atd
sudo systemctl enable atd
Usage
echo "echo 'Hello World' >> /tmp/at_test.log" | at now + 2 minutes
now + 2 minutes→ natural language time formatJobs go into a queue (
atq,atrm <jobid>)
Missed Jobs with anacron
cron assumes your system is always on. If the system is off, jobs are skipped. anacron solves this by running jobs once the system is back online.
Config file:
/etc/anacrontabSyntax:
period delay job-identifier commandExample:
1 10 daily_cleanup /home/user/cleanup.sh→ Run once daily, wait 10 minutes after boot.
Modern Scheduling with systemd Timers
Most modern distros (CentOS 7+, Ubuntu 16+) use systemd. Instead of cron, you can use timers.
Example: Daily Job
Service file
/etc/systemd/system/myjob.service[Unit] Description=My Custom Job [Service] ExecStart=/home/user/backup.shTimer file
/etc/systemd/system/myjob.timer[Unit] Description=Run backup script daily [Timer] OnCalendar=daily Persistent=true [Install] WantedBy=timers.targetEnable & Start
sudo systemctl enable --now myjob.timer systemctl list-timers
Cheatsheet: Quick Scheduling Commands
| Tool | Purpose | Key Commands |
|---|---|---|
| at | One-time jobs | at TIME, atq, atrm <jobid> |
| cron | Recurring jobs | crontab -e, crontab -l, systemctl status cron |
| anacron | Recurring jobs, catch missed | Edit /etc/anacrontab, anacron -T |
| systemd timers | Modern recurring jobs | systemctl list-timers, .timer units |
Practice Exercises
Beginner
Schedule a job with
atthat writes "Take a break!" into/tmp/reminder.logafter 1 minute.Create a cron job that runs every 5 minutes and writes the current date into
/tmp/date.log.
Intermediate
Use
anacronto run a cleanup script daily, but delay execution for 15 minutes after boot.Write a cron job that runs only on Mondays at 9:30 AM.
Advanced
Create a systemd timer that runs
echo "Systemd Rocks"into/tmp/systemd.logevery hour.Compare the execution of a cron job vs. a systemd timer — which one provides better logging?
Now that you can schedule and automate tasks, the next step is understanding storage. In the next article, we dive into: Linux Disk Management — exploring partitions, filesystems, mounting, and how to monitor disk health.
