Linux Fundamentals - Part 10: Bash Scripting (Crontab)
Source: Dev.to
Crontab
Crontab (cron table) is 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. The cron daemon checks the tables of scheduled tasks and executes them accordingly. Each user can have their own crontab file, making cron jobs flexible for both system‑wide and user‑specific automation.
Editing a crontab
crontab -e
This opens your crontab file in the default editor. Each line in the file represents a scheduled task using the following 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)
Example Schedules
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
Special Keywords
Cron provides shortcuts for common schedules:
| Keyword | Meaning |
|---|---|
@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
Managing Your Crontab
-
List all cron jobs for the current user:
crontab -l -
Edit the crontab again (e.g., with Vim):
crontab -e
Making 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.