One of the primary reasons system administrators love Linux is its ability to run completely autonomously. You do not need to manually log in to a server every Friday at midnight to run a database backup, clear a log file, or synchronize a directory. You can automate all of these repetitive tasks using the built-in, time-based job scheduler known as the cron daemon.
What is the cron Daemon?
cron is a background service (daemon) that runs continuously on almost every Linux distribution. Its sole purpose is to wake up every minute, check a configuration file (called a crontab), and execute any scripts or commands scheduled for that precise moment. It is incredibly reliable and requires virtually zero system resources.
How to Access Your Crontab
Every user on a Linux system can have their own personal schedule of tasks. To edit your schedule, you must use the crontab tool.
Open your terminal and type:
crontab -e
If this is your first time running the command, Linux may ask you to choose a text editor (Nano is highly recommended for beginners). Once the editor opens, you will see a file full of commented text explaining how the system works. You add your scheduled tasks to the very bottom of this file.
Understanding the Syntax (The Five Asterisks)
A cron job requires exactly six pieces of information. The first five dictate the schedule, and the sixth is the absolute path to the command or script you want to run.
The time format looks like this: * * * * *
From left to right, the asterisks represent:
- Minute (0 – 59)
- Hour (0 – 23, in military time)
- 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.” Therefore, if you write * * * * * /path/to/script.sh, the script will run every single minute, of every hour, of every day.
Common Scheduling Examples
Here are a few practical examples to help you understand the syntax:
Run a backup script every day at exactly midnight (00:00):
0 0 * * * /usr/local/bin/backup.sh
(Minute 0, Hour 0, every day, every month, every weekday).
Run a disk cleanup command every Friday at 3:30 PM:
30 15 * * 5 /home/user/cleanup.sh
(Minute 30, Hour 15, every day of the month, every month, on weekday 5 (Friday)).
Run a server health check every 15 minutes:
You can use the slash symbol (/) to dictate intervals.
*/15 * * * * /var/scripts/healthcheck.sh
Crucial Rules for Writing Cron Jobs
- Always use absolute paths: The
crondaemon runs in a very restricted environment. It does not know what your current working directory is, and it does not load your normal PATH variables. Always write the full, absolute path to both the command (e.g.,/usr/bin/python3instead of justpython3) and the script. - Ensure scripts are executable: If you schedule a bash script, make sure you have given it execute permissions (
chmod +x script.sh), otherwisecronwill fail silently.
Once you finish writing your schedule, save and exit the text editor. The system will print “crontab: installing new crontab,” confirming your tasks are now fully automated.