Managing a Linux server requires performing routine maintenance, such as backing up databases, rotating server logs, or running security scans. If you rely on a human administrator to log in via SSH and execute these scripts manually every night at 2:00 AM, mistakes will inevitably happen. To automate any script or command on a precise schedule, you must master the Linux cron daemon.
The cron daemon is a background service that wakes up every single minute, checks a text file (called a crontab) for any scheduled jobs, and executes them silently. In this guide, you will learn how to write cron expressions and schedule your automated tasks.
Accessing the Crontab
You do not edit the cron configuration files directly with a text editor. Instead, you use a dedicated command that ensures the syntax is checked before saving.
Open your terminal and type:
crontab -e
If this is your first time running the command, Linux will ask you to choose a default text editor (usually nano or vim). Once selected, the crontab file will open.
Understanding Cron Syntax
Every line in a crontab represents a single scheduled job. The syntax is strictly divided into six columns. The first five columns define when the job should run, and the sixth column defines what command to execute.
The five time columns are:
- Minute (0-59)
- Hour (0-23)
- Day of the Month (1-31)
- Month (1-12)
- Day of the Week (0-7, where both 0 and 7 represent Sunday)
An asterisk (*) means “every” or “always”.
Example 1: Running a Daily Backup
If you want a bash script named backup.sh to run every single day at exactly 2:30 AM, your cron entry would look like this:
30 2 * * * /home/user/scripts/backup.sh
This translates to: Minute 30, Hour 2, Every Day, Every Month, Every Day of the Week.
Example 2: Running a Job Every 15 Minutes
To run a task multiple times an hour (e.g., checking if a web server is still online), you use the division operator (/).
*/15 * * * * /usr/bin/php /var/www/check_status.php
This translates to: Every 15 minutes, Every Hour, Every Day, Every Month, Every Day of the Week.
Example 3: Running a Job on Specific Days
If you only want a report generated on Monday mornings at 9:00 AM, you use the Day of the Week column (Monday is 1).
0 9 * * 1 /home/user/scripts/report.sh
Crucial Rules for Cron Jobs
When administrators complain that a script works perfectly when they run it manually but fails when scheduled via cron, it is almost always due to one of two reasons:
- Absolute Paths:
cronruns in a very restricted, minimal environment. It does not load your user profile’s$PATHvariables. Therefore, you must use absolute paths for everything. Instead of typingpython script.py, you must type the full path:/usr/bin/python3 /home/user/script.py. - Executable Permissions: The file you are trying to run must have execute permissions. (Run
chmod +x script.shbefore scheduling it).
Once you add your line to the crontab -e window, simply save and exit the text editor. The cron daemon will immediately read the new file and begin executing your automated schedule.