The Need for Invisible Automation
If you write a brilliant shell script that securely backs up your database to an offsite server, that script is useless if you have to log in every single night at 2:00 AM to manually type the command and hit Enter.
Linux solves this problem with a built-in background daemon called cron. The cron daemon wakes up every single minute, checks a massive list of schedules, and automatically executes any command that is due to run.
To view, create, or modify these automated schedules for your specific user account, you use the crontab (Cron Table) command.
1. Editing the Cron Table
To open your personal list of scheduled tasks, open the terminal and type:
crontab -e
(The -e stands for “edit”).
This will open a text file in your default terminal editor (usually nano or vim). If this is your first time running the command, the file will be mostly empty, containing only commented-out instructions (lines starting with #).
2. The Syntax of Time
The hardest part of using crontab is understanding its unique, five-asterisk time format. Every scheduled task must begin with exactly five fields separated by spaces, followed by the command you want to run.
The five fields represent, in order:
- Minute (0 – 59)
- Hour (0 – 23, where 0 is Midnight)
- Day of the Month (1 – 31)
- Month (1 – 12)
- Day of the Week (0 – 7, where both 0 and 7 are Sunday)
An asterisk (*) means “every.”
Example A: The Nightly Backup
If you want to run your backup script every single day at exactly 2:30 AM, you write this line in your crontab file:
30 2 * * * /home/jsmith/scripts/backup.sh
Translation: Run at the 30th minute, of the 2nd hour, every day, every month, every day of the week.
Example B: The Weekly Maintenance
If you want to run a server cleanup script only on Sundays at exactly Midnight:
0 0 * * 0 /usr/bin/python3 /home/jsmith/cleanup.py
Example C: The Five-Minute Poller
If you have a script that checks a website’s API to ensure it hasn’t crashed, you might want it to run very frequently. You can use the division slash (/) to create intervals.
To run a command every 5 minutes:
*/5 * * * * curl https://api.mywebsite.com/healthcheck
3. The Golden Rule: Absolute Paths
The most common mistake beginners make is assuming cron knows where their files are. When you log into Linux, the system loads a massive amount of environmental variables (like the $PATH), so you can just type python3 script.py and it works.
The cron daemon is basically a blind robot. It does not load your personal environment. Therefore, you must use absolute paths for absolutely everything.
Instead of typing python3 script.py, you must type the full path to the executable and the full path to the script: /usr/bin/python3 /home/jsmith/script.py.
4. Auditing Your Schedules
If you just want to see what is currently scheduled to run without risking accidental edits, use the -l (list) flag.
crontab -l
If you want to completely erase your entire schedule (be very careful with this), use the -r (remove) flag.
crontab -r
Conclusion
The crontab command is the foundation of Linux automation. By mastering its five-field time syntax and strictly adhering to absolute file paths, administrators can build robust, “set-it-and-forget-it” pipelines that maintain servers while they sleep.