Linux Automation

Cron Jobs &
Task Scheduling

How Linux runs backups at 2am without anyone touching a keyboard. The scheduler behind every production server's automation.

Sections

5

Read time

15 min

Level

Beginner

What is a Cron Job?

A cron job is a scheduled task β€” a command that runs automatically at a time you specify. No manual intervention. No staying up at 2am to run the backup.

πŸ’‘
Think of it this way...
Alarm clock = runs at 7am every day β†’ wakes you up
Cron job = runs at 7am every day β†’ checks disk space, or sends a report, or cleans up old files

What companies use cron for

Every minuteCheck if services are running, collect metrics
Every hourAggregate logs, send alerts if something looks wrong
Every day at 2amRun database backups (off-peak, low traffic)
Every Sunday midnightClean up old log files, rotate archives
Every Monday 9amSend weekly performance reports to stakeholders
Every month 1stGenerate billing reports, renew SSL certificates
πŸ’‘

Key Takeaway

Every production Linux server has dozens of cron jobs running. It's how sysadmin work gets automated β€” the server does maintenance while you sleep.

The Schedule Format

Every cron job has a schedule written in this exact format. Five fields, then the command:

* * * * * command-to-run

β”‚

β”‚

β”‚

β”‚

β”‚

Minute

0–59

Hour

0–23

Day

1–31

Month

1–12

Weekday

0=Sun..6=Sat

* means "every" β€” every minute, every hour, every day, etc.

The * wildcard means "every possible value." So * * * * * runs every single minute of every day.

🀯

Did You Know?

The name β€œcron” comes from the Greek word β€œchronos” meaning time. The program that reads and executes crontabs is the β€œcron daemon” (crond), which wakes up every minute to check if any jobs need to run.

Reading the Format

Click any example below to see how the format breaks down:

* * * * *β†’Every single minute
*Minute0–59
*Hour0–23
*Day1–31
*Month1–12
*Weekday0=Sun, 6=Sat
*

every (all values)

*/5

every 5 of that unit

0

at exactly 0 (midnight, top of hour)

1-5

a range (Monday through Friday)

1,3,5

specific values (Mon, Wed, Fri)

Managing Crontabs

Each user has their own crontab β€” a file listing their scheduled jobs. You manage it with the crontab command:

Terminal
▢️

Click the Play button above to start!

Watch the commands execute step by step

πŸ’‘

Key Takeaway

crontab -r removes ALL your cron jobs with no confirmation prompt. Unlike most destructive commands, there's no "are you sure?" Always double-check with crontab -l first.

Real Crontab Examples

Here's what a typical sysadmin's crontab looks like:

# crontab -l output

*/5 * * * */home/charith/scripts/collect-metrics.sh← every 5 min
0 * * * */home/charith/scripts/check-services.sh← every hour
0 2 * * */home/charith/scripts/backup.sh← 2am daily
0 0 * * 0/home/charith/scripts/clean-logs.sh← Sunday midnight
0 9 * * 1-5/home/charith/scripts/send-report.sh← 9am weekdays

Logging cron output

By default, cron emails output to the local user β€” which nobody reads. Better practice: redirect to a log file:

Terminal
▢️

Click the Play button above to start!

Watch the commands execute step by step

Key Concepts Flashcards

Click any card to flip and reveal the definition.

What is a cron job?

A scheduled task β€” a command that runs automatically at a time you specify. Like setting an alarm, but for scripts.

Cron format

* * * * * command β€” minute(0-59) hour(0-23) day-of-month(1-31) month(1-12) day-of-week(0=Sun-6=Sat)

What does * mean in cron?

'Every'. * in the hour field = every hour. * in the month field = every month. All * = 'every minute of every day'.

*/5 * * * *

Every 5 minutes. The */ syntax means 'every N of that unit'. */5 in minutes = every 5 minutes.

0 2 * * *

Every day at 2am. 0 = minute 0 (on the hour), 2 = 2am, * * * = any day/month/weekday.

How to edit cron jobs?

crontab -e (edit), crontab -l (list), crontab -r (remove ALL β€” be careful!). Each user has their own crontab file.

Where do production cron outputs go?

By default, cron emails output to the local user. Better practice: redirect to a log file: */5 * * * * /path/script.sh >> /var/log/myscript.log 2>&1