The Complete Beginner's Guide to Cron Jobs
Cron is one of the most powerful tools a developer or sysadmin can master. This guide takes you from "what is cron?" to writing, testing, and debugging your own scheduled tasks.
What is a Cron Job?
A cron job is a scheduled task that runs automatically on Unix/Linux systems at a specified time interval. The name comes from Chronos, the Greek god of time. Cron is a daemon (background process) that wakes up every minute, checks its schedule table called crontab, and runs any tasks whose scheduled time matches the current time.
Cron is used for:
- ✓Running database backups every night at 2 AM
- ✓Sending digest emails every Monday morning
- ✓Clearing temp files every hour
- ✓Syncing data from an external API every 15 minutes
- ✓Generating reports on the 1st of every month
- ✓Triggering CI/CD pipelines on a schedule
Key Terms
cronThe background daemon that runs scheduled jobs
crontabThe file (and command) that contains your cron schedule
cron expressionThe 5-field timing string like `*/5 * * * *`
cron jobA task entry in crontab — expression + command
Cron Syntax: 5 Fields Explained
Every cron expression consists of exactly 5 fields separated by spaces, followed by the command to run:
*means "every valid value for this field"Minute
(0–59)0At minute 0 (top of the hour)30At minute 30 (half past)*/5Every 5 minutes15,45At minute 15 and minute 45Hour
(0–23 (0 = midnight))0Midnight (12:00 AM)99 AM*/6Every 6 hours (midnight, 6am, noon, 6pm)8-17Every hour from 8 AM to 5 PMDay of Month
(1–31)11st day of the month1515th day of the monthLLast day of the month (some cron flavors)1,151st and 15thMonth
(1–12 (or JAN–DEC))1January12December3,6,9,12March, June, September, December (quarters)*Every monthDay of Week
(0–6 (0 = Sunday) or SUN–SAT)0Sunday (also 7 on some systems)1Monday1-5Monday through Friday (weekdays)6SaturdaySpecial Characters
Cron expressions support several special characters that give you flexible scheduling control:
| Character | Meaning | Example | Result |
|---|---|---|---|
* | Any / Every | * * * * * | Every minute |
, | List | 0 9,17 * * * | At 9 AM and 5 PM |
- | Range | 0 9-17 * * * | Every hour from 9 AM to 5 PM |
*/n | Step / Every n | */15 * * * * | Every 15 minutes |
@hourly | Macro: hourly | @hourly | Same as `0 * * * *` |
@daily | Macro: daily midnight | @daily | Same as `0 0 * * *` |
@weekly | Macro: Sunday midnight | @weekly | Same as `0 0 * * 0` |
@monthly | Macro: 1st of month | @monthly | Same as `0 0 1 * *` |
@reboot | Run at system startup | @reboot | Runs once when cron starts |
10 Common Cron Schedules
These are the most frequently used cron expressions that cover 90% of real-world use cases:
* * * * **/5 * * * *0 * * * *0 0 * * *0 9 * * *0 9 * * 1-50 0 * * 10 9 * * 10 0 1 * *0 0 1 1 *Writing Your First Cron Job
On Linux, your personal cron jobs are managed with the crontab command. Here's how to add your first scheduled task:
Open your crontab file
crontab -eThis opens your user's crontab in the default editor. Use `sudo crontab -e` for system-wide cron (use with caution).
Add a cron job entry
# Run backup.sh every day at 2 AM
0 2 * * * /home/user/scripts/backup.sh >> /home/user/logs/backup.log 2>&1Each line is one job: expression + command. The `>> file 2>&1` part redirects both stdout and stderr to a log file.
Save and exit
# In nano: Ctrl+X, then Y, then Enter
# In vim: :wqCron automatically installs the new schedule — no restart needed.
Verify it was saved
crontab -lLists all your current cron jobs. Confirm your new entry appears.
💡 Pro Tip: Use absolute paths everywhere
Cron runs with a minimal PATH. Always use full paths for both the script and any commands inside it. Instead of backup.sh, use /home/user/scripts/backup.sh. Instead of python, use /usr/bin/python3.
Environment Variables in Cron
Cron runs in a restricted environment — it does not load your shell's .bashrc or .profile. This is the #1 reason cron jobs that "work on the command line" fail silently in cron.
You can set environment variables at the top of your crontab:
# Set environment variables for all cron jobs
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAILTO="" # Suppress email on output (or set to your email)
HOME=/home/user # Set home directory
# Your cron jobs below
0 2 * * * /home/user/scripts/backup.shAlternatively, source your profile at the start of each script: source ~/.bashrc or pass the full environment explicitly using /usr/bin/env.
Debugging Cron Jobs
When cron jobs don't run as expected, here's how to diagnose the problem:
Check the cron log
# On Ubuntu/Debian
grep CRON /var/log/syslog | tail -20
# On CentOS/RHEL
grep CRON /var/log/cron | tail -20Cron logs every job execution. If your job isn't showing up here, it wasn't triggered — check the expression.
Redirect output to a log file
*/5 * * * * /path/to/script.sh >> /tmp/cron_debug.log 2>&1The `2>&1` redirects stderr to stdout, so you capture error messages too.
Test the command manually
# Run with the same environment cron uses
env -i HOME=/home/user PATH=/usr/bin:/bin /path/to/script.shIf this fails but the script works normally, it's an environment/path issue.
Verify cron daemon is running
# Check cron service status
systemctl status cron # Ubuntu/Debian
systemctl status crond # CentOS/RHELCron must be running to execute jobs. Start it with `systemctl start cron` if stopped.
Cron Job Best Practices
Use absolute paths
Always use full paths for scripts and commands. Cron has a minimal PATH.
Log everything
Redirect stdout and stderr to log files. Silent failures are hard to debug.
Add a lock file for long jobs
Use flock or check for a PID file to prevent overlapping executions.
Handle timezones explicitly
Cron uses the system timezone. Set TZ= in your crontab or use a UTC-aware scheduler.
Stagger jobs
Avoid scheduling many jobs at exact :00 times. Use offsets like "*/5 1-6 * * *" to spread load.
Test expressions first
Use a cron expression validator before deploying to avoid scheduling mistakes.
Set MAILTO for alerts
Set MAILTO="" to suppress emails, or MAILTO=you@example.com for error notifications.
Use cron wrappers for production
Consider tools like Supercronic, cronic, or a scheduler service for better observability.
Beyond Linux: Cron on Other Platforms
Cron expression syntax has been adopted by many platforms beyond Linux crontab. The core 5-field format is used almost universally, but there are platform-specific differences:
GitHub Actions
Uses on.schedule with cron: key. Always UTC. Minimum every 5 minutes.
View platform guide →AWS EventBridge
Supports both cron() and rate() expressions. Uses 6-field format (adds year). UTC only.
View platform guide →Kubernetes CronJob
Standard 5-field cron syntax. Add startingDeadlineSeconds and concurrencyPolicy.
View platform guide →GCP Cloud Scheduler
Full timezone support with the timeZone field. Uses App Engine cron syntax.
View platform guide →Put it into practice
Use our free tools to write, test, and validate your cron expressions instantly.