This guide explains how to automatically schedule tasks on Linux systems using Cron, Anacron, and At for reliable process automation.
In this guide, we explain how to automatically schedule tasks on Linux systems, ensuring processes like database backups run reliably without manual intervention. You can achieve task automation using three main utilities: Cron, Anacron, and At. Each method is designed for different scheduling scenarios, and in this article, we break down their usage step by step.
Cron is ideal for repetitive tasks that need to run at specific intervals—whether every few minutes, hours, or on selected days. It enables administrators to define the exact times for running automated commands.The syntax for a cron job might appear challenging at first, but the system-wide crontab file provides excellent guidance. Located at /etc/crontab, this file includes comments that explain the format. Remember, instead of modifying the system-wide crontab, each user should use their personal crontab.An excerpt from /etc/crontab shows the scheduling syntax:
Copy
Ask AI
$ cat /etc/crontabSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root# Example of job definition:# --------------------------- minute (0 - 59)# | ------------------------- hour (0 - 23)# | | ----------------------- day of month (1 - 31)# | | | --------------------- month (1 - 12) OR jan,feb,mar,apr ...# | | | | ------------------- day of week (0 - 6) (Sunday=0 or 7) OR# sun,mon,tue,wed,thu,fri,sat# | | | | |# * * * * * user-name command to be executed35 6 * * * root /bin/some_command --some_options
In a cron job, the first five fields define the schedule:
Instead of editing the system-wide crontab, you should update your personal crontab. To do so, run:
Copy
Ask AI
$ crontab -e
Before adding commands, always confirm you are using full paths. To locate the full path for the touch command:
Copy
Ask AI
$ which touch/usr/bin/touch
Now, to schedule the touch command to run daily at 6:35 A.M. and create a file named test_passed, include this line in your personal crontab (note that there is no username for user-specific crontabs):
Copy
Ask AI
35 6 * * * /usr/bin/touch test_passed
Below are additional examples of cron entries:
Copy
Ask AI
$ crontab -e35 6 * * * /usr/bin/touch test_passed # Daily at 6:35 AM0 3 * * 0 /usr/bin/touch test_passed # Every Sunday at 3:00 AM0 3 * * 7 /usr/bin/touch test_passed # Also every Sunday at 3:00 AM (alternate notation)0 3 15 * * /usr/bin/touch test_passed # On the 15th of every month at 3:00 AM0 3 * * * /usr/bin/touch test_passed # Every day at 3:00 AM0 * * * * /usr/bin/touch test_passed # At the start of every hour
To view your current crontab entries:
Copy
Ask AI
$ crontab -l35 6 * * * /usr/bin/touch aaron_test
For root user’s crontab entries, prefix the command with sudo:
Anacron is built for tasks that need to run on a daily, weekly, or monthly basis—even if the computer was off at the scheduled time. Unlike Cron, Anacron’s smallest time unit is one day. This means if a scheduled job is missed (for example, if the computer was off at noon), it will execute as soon as possible after the system restarts.To schedule a task with Anacron, you need to update the Anacrontab file. Open it with your favorite text editor:
Copy
Ask AI
$ sudo vim /etc/anacrontab#period in days delay in minutes job-identifier command1 5 cron.daily nice run-parts7 25 cron.weekly nice run-parts@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
Each entry consists of:
The period (in days) between executions.
The delay (in minutes) after the system starts before running the job.
A unique identifier for the job.
The exact command to run (always use full paths).
For instance, to schedule a job every 3 days with a 10-minute delay:
Copy
Ask AI
$ sudo vim /etc/anacrontab#period in days delay in minutes job-identifier command1 5 cron.daily nice run-parts7 25 cron.weekly nice run-parts@monthly 45 cron.monthly nice run-parts /etc/cron.monthly3 10 test_job /usr/bin/touch /root/anacron_created_this
If multiple jobs are due at the same time, assign different delays to prevent system overload.
You can also test your Anacrontab syntax with the -T option. A lack of error messages indicates a correct configuration.
The At command is suited for one-off tasks that need to run at a specific time or after a given interval. When scheduling a job with At, always use the 24-hour format.
To remove a scheduled job, use the job ID with the atrm command:
Copy
Ask AI
$ atrm 20
This concludes our comprehensive guide on scheduling tasks in Linux using Cron, Anacron, and At. Apply these techniques to automate your system tasks and enhance your operational efficiency.For further reading, check out these resources: