Crontab
Crontab stands for cron table. It’s a text file used to schedule commands or scripts to run automatically at specified times on Unix-like systems. These scheduled tasks are called cron jobs.
How Cron Jobs Work
The cron daemon checks tables of scheduled tasks and executes them accordingly. Each user can have their own crontab file. This makes cron jobs very flexible for both system-wide and user-specific automation.
Crontab Syntax
crontab -e
This opens your crontab file in your default editor.
Each line in the file represents a scheduled task using this format:
* * * * * /full/path/to/command
| | | | |
| | | | └─ Day of Week (0–7) (Sunday = 0 or 7)
| | | └─── Month (1–12)
| | └───── Day of Month (1–31)
| └─────── Hour (0–23)
└───────── Minute (0–59)
Crontab Examples
Run a Script Every Day at 6 AM
0 6 * * * /home/user/backup.sh
Run Every 10 Minutes
*/10 * * * * /usr/bin/python3 /home/user/script.py
Run Every Monday at Midnight
0 0 * * 1 /home/user/monday_job.sh
Run at 3:30 PM on the First Day of the Month
30 15 1 * * /home/user/monthly_report.sh
Cron Shortcuts
Cron provides special keywords to make schedules easier:
@reboot - Run at system startup
@hourly - Run every hour
@daily - Run once per day
@weekly - Run once per week
@monthly - Run once per month
@yearly - Run once per year
Example:
@daily /home/user/daily_cleanup.sh
Viewing Your Cron Jobs
To list all cron jobs for your user:
crontab -l
To edit again (with Vim):
crontab -e
Tips for Cron Jobs
Always use full paths.
Example:
/usr/bin/python3 /home/user/script.py
Make scripts executable.
chmod +x /home/user/script.sh
Today, I learned how to use crontab to schedule tasks in Linux. I discovered how cron jobs work, how to define time schedules using cron syntax, and how to manage scheduled tasks using Vim as my editor.
Top comments (0)