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.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Cron
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:
- Minute: 0–59
- Hour: 0–23 (0 indicates midnight, 23 indicates 11 PM)
- Day of the Month: 1–31
- Month: 1–12 (or short month names such as jan, feb, etc.)
- Day of the Week: 0–6 (0 or 7 represents Sunday; short names are also allowed, e.g., mon, tue)
- An asterisk (*) represents all possible values.
- A comma (,) separates multiple values.
- A dash (-) specifies a range (e.g., “2-4” means 2, 3, and 4).
- A slash (/) represents step values, for example, “*/4” runs every 4 units.
Editing a User’s Crontab
Instead of editing the system-wide crontab, you should update your personal crontab. To do so, run:touch command:
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):
sudo:
-u option combined with sudo:
Using Special Cron Directories
Cron also provides job scheduling using special directories:/etc/cron.daily/etc/cron.hourly/etc/cron.weekly/etc/cron.monthly
-
Create the script:
-
Copy it to the hourly directory with root privileges:
-
Set the correct permissions for execution:
-
To remove this scheduled job, simply delete the script:
Always use full paths in your cron jobs to prevent issues related to the environment’s PATH variable.
Anacron
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:- 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).
If multiple jobs are due at the same time, assign different delays to prevent system overload.
-T option. A lack of error messages indicates a correct configuration.
At
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.Scheduling a Job with At
To schedule a job at a specific time, follow these steps:-
Start by entering the at command:
-
Enter the command you want to execute (ensure you use the full path):
- Press Enter on an empty line and then press Control-D to save the job.
Managing At Jobs
To review scheduled At jobs, use: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: