@Ajit5ingh

Cron Jobs Explained

Schedule tasks to run automatically on Linux

What is a Cron Job?

A cron job is a scheduled task that runs automatically at specific times on Linux and Unix systems. Want to back up your database every night at 2 AM? Or clear temp files every Sunday? That's what cron does. You write the schedule, cron handles the rest.

Fun fact: "Cron" comes from the Greek word "chronos" meaning time. It's been around since the 1970s.

How Cron Works


flowchart LR
    A[Crontab File] --> B[Cron Daemon]
    B --> C{Time Match?}
    C -->|Yes| D[Run Command]
    C -->|No| E[Wait]
    E --> C
    D --> F[Log Output]

The cron daemon runs in the background, checking every minute if any scheduled tasks need to run.

Cron Expression Format

A cron expression has 5 fields separated by spaces. Each field tells cron when to run:

* Minute
0-59
* Hour
0-23
* Day
1-31
* Month
1-12
* Weekday
0-6

šŸ’” Tip: An asterisk (*) means "every" - so * * * * * runs every minute.

Special Characters

* Asterisk

Matches every value. * * * * * = every minute of every hour of every day.

, Comma

List multiple values. 0 9,17 * * * = at 9 AM and 5 PM.

- Hyphen

Range of values. 0 9-17 * * * = every hour from 9 AM to 5 PM.

/ Slash

Step values. */15 * * * * = every 15 minutes.

Build Your Cron Expression

Not sure if you got the syntax right? Use our free tool to parse or build cron expressions.

Try Cron Expression Tool →

Common Examples

0 0 * * *

Every day at midnight. Good for daily backups or cleanup tasks.

0 9 * * 1-5

Every weekday at 9 AM. Send daily reports, skip weekends.

*/10 * * * *

Every 10 minutes. Health checks or monitoring.

0 2 * * 0

Every Sunday at 2 AM. Weekly maintenance window.

0 0 1 * *

First day of every month at midnight. Monthly reports or billing.

Managing Cron Jobs

Use the crontab command to manage your scheduled tasks:

# View your cron jobs
crontab -l

# Edit your cron jobs
crontab -e

# Remove all your cron jobs (careful!)
crontab -r

āš ļø Watch out: Cron uses the system timezone. If your server is in UTC but you want 9 AM New York time, you'll need to convert.

Crontab File Format


flowchart TB
    subgraph crontab[Crontab Entry]
        A["* * * * *"] --> B["/path/to/script.sh"]
    end
    
    A --> C[Schedule]
    B --> D[Command to Run]
    
    C --> E["When to run:
minute, hour, day, month, weekday"] D --> F["What to run:
script, command, or program"]

Where Cron is Used

  • Database Backups: Run pg_dump or mysqldump every night.
  • Log Rotation: Clean up old log files to save disk space.
  • Email Reports: Send daily or weekly summary emails.
  • Data Sync: Pull data from APIs or sync with external services.
  • Health Checks: Monitor services and alert if something's down.

Beyond Linux Cron

The cron syntax is used everywhere, not just Linux:

  • GitHub Actions - Schedule workflows with cron syntax
  • AWS CloudWatch Events - Trigger Lambda functions on schedule
  • Kubernetes CronJobs - Run containerized tasks on schedule

Subscribe via RSS Feed

Add to Feedly, Inoreader, or your favorite RSS reader

Get RSS Feed
← Back to All Explainers