If you need your Linux server to automatically back up a database every night at 2:00 AM, or empty a temporary cache folder every Friday evening, you do not need to stay awake and manually type those commands. Instead, you can use the built-in crontab command to schedule incredibly precise, recurring background tasks.
What is a Crontab?
The “cron” daemon is a background service that constantly checks the system clock. The “crontab” (cron table) is the specific text file where you write your schedule. Every user on a Linux system can have their own personal crontab file.
How to Edit Your Schedule
- Open your Linux terminal.
- Type the following command to open your specific cron schedule in a text editor:
crontab -e
If this is your very first time running the command, Linux may ask you to choose a default text editor (like nano or vim). Select nano for the easiest experience.
Understanding the Cron Syntax
At the bottom of the text file, you write your scheduled task. Every cron job must strictly follow a five-part time format, followed immediately by the command you want to run.
The five time fields represent:
- Minute (0-59)
- Hour (0-23, using a 24-hour clock)
- Day of the Month (1-31)
- Month (1-12)
- Day of the Week (0-7, where both 0 and 7 represent Sunday)
You use an asterisk (*) as a wildcard to mean “every.”
Examples of Scheduled Tasks
To run a backup script located at /home/user/backup.sh every single day at exactly 2:30 AM, you would write:
30 2 * * * /home/user/backup.sh
To run a cache-clearing command every Monday at 5:00 PM (17:00), you would write:
0 17 * * 1 rm -rf /tmp/cache/*
To run a system health check every single minute of every single day, you would use five asterisks:
* * * * * /home/user/health_check.sh
Once you save the file and exit the editor, the cron daemon will automatically read your new schedule and begin executing the tasks silently in the background.