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:
0-59
0-23
1-31
1-12
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
Every day at midnight. Good for daily backups or cleanup tasks.
Every weekday at 9 AM. Send daily reports, skip weekends.
Every 10 minutes. Health checks or monitoring.
Every Sunday at 2 AM. Weekly maintenance window.
First day of every month at midnight. Monthly reports or billing.
Managing Cron Jobs
Use the crontab command to manage your scheduled tasks:
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