Make Linux run commands automatically.
A cron job is a command that Linux runs automatically at a scheduled time.
Instead of remembering:
"Run the backup every night at 11 PM."
you tell Linux:
"Run this command every night at 11 PM."
1. The Basic Idea
A cron job has two parts:
WHEN?
+
WHAT?
Example:
0 23 * * * /home/user/backup.sh
Meaning:
Run
backup.shevery day at 11:00 PM.
2. Open Your Cron Jobs
Run:
crontab -e
This opens your personal cron schedule.
To see your existing jobs:
crontab -l
3. The Five Time Fields
A cron schedule starts with five timing fields:
* * * * *
│ │ │ │ │
│ │ │ │ └── Day of week
│ │ │ └──── Month
│ │ └────── Day of month
│ └──────── Hour
└────────── Minute
Think:
MINUTE
HOUR
DAY
MONTH
WEEKDAY
4. The * Means "Every"
For example:
* * * * *
means:
Every minute.
This:
0 * * * *
means:
At minute 0 of every hour.
5. Common Examples
Every minute
* * * * * command
Every hour
0 * * * * command
Every day at midnight
0 0 * * * command
Every day at 8 AM
0 8 * * * command
Every day at 11:30 PM
30 23 * * * command
Every Monday at 9 AM
0 9 * * 1 command
Every Sunday at 6 PM
0 18 * * 0 command
6. Weekdays
The day-of-week field commonly uses:
0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday
So:
0 9 * * 1-5 command
means:
Run at 9 AM, Monday to Friday.
7. Every X Minutes
You can use /.
Every 5 minutes:
*/5 * * * * command
Every 15 minutes:
*/15 * * * * command
Every 30 minutes:
*/30 * * * * command
8. Multiple Times
You can specify multiple values.
For example:
0 8,12,18 * * * command
means:
Run at 8 AM, 12 PM and 6 PM.
9. Add a Cron Job
Open the editor:
crontab -e
Add a line:
0 2 * * * /home/user/backup.sh
Save and exit.
Your job is now scheduled.
10. Use Full Paths
Cron has a limited environment compared with your normal terminal.
Instead of:
backup.sh
prefer:
/home/user/backup.sh
And instead of relying on a command being found automatically:
python script.py
you may need:
/usr/bin/python3 /home/user/script.py
Full paths make cron jobs more reliable.
11. Make Scripts Executable
If you're running a script:
chmod +x backup.sh
Then:
0 2 * * * /home/user/backup.sh
12. Save the Output
If you want to keep a record of what the command produces:
0 2 * * * /home/user/backup.sh >> /home/user/backup.log 2>&1
This sends output and errors into:
backup.log
Very useful when troubleshooting.
13. Common Cron Uses
Cron is often used for:
Backups
↓
Database maintenance
↓
Reports
↓
Data imports
↓
Cleanup tasks
↓
Notifications
↓
Scheduled scripts
14. Check Your Cron Jobs
crontab -l
If you see:
0 2 * * * /home/user/backup.sh
your job is scheduled for 2 AM every day.
15. Remove a Cron Job
Open your crontab:
crontab -e
Delete the line you no longer want.
Save and exit.
16. A Simple Example
Suppose you have:
/home/user/backup.sh
and want it to run every night at 11 PM.
Add:
0 23 * * * /home/user/backup.sh
That's it.
Quick Reference
| Schedule | Cron |
|---|---|
| Every minute | * * * * * |
| Every hour | 0 * * * * |
| Every day at midnight | 0 0 * * * |
| Every day at 8 AM | 0 8 * * * |
| Every day at 11:30 PM | 30 23 * * * |
| Every Monday at 9 AM | 0 9 * * 1 |
| Mon–Fri at 9 AM | 0 9 * * 1-5 |
| Every 5 minutes | */5 * * * * |
| Every 15 minutes | */15 * * * * |
The Golden Rule
Remember the five fields:
┌───── Minute
│ ┌─── Hour
│ │ ┌─ Day
│ │ │ ┌─ Month
│ │ │ │ ┌─ Weekday
│ │ │ │ │
* * * * *
Then ask:
"What command do I want Linux to run, and when?"
That's a cron job.
