BeginnerSyntaxLinux· 12 min read

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

cron

The background daemon that runs scheduled jobs

crontab

The file (and command) that contains your cron schedule

cron expression

The 5-field timing string like `*/5 * * * *`

cron job

A 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:

MINUTE
0–59
HOUR
0–23
DAY (month)
1–31
MONTH
1–12
DAY (week)
0–6
*****
↑ Each *means "every valid value for this field"

Minute

(0–59)
0At minute 0 (top of the hour)
30At minute 30 (half past)
*/5Every 5 minutes
15,45At minute 15 and minute 45

Hour

(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 PM

Day of Month

(1–31)
11st day of the month
1515th day of the month
LLast day of the month (some cron flavors)
1,151st and 15th

Month

(1–12 (or JAN–DEC))
1January
12December
3,6,9,12March, June, September, December (quarters)
*Every month

Day of Week

(0–6 (0 = Sunday) or SUN–SAT)
0Sunday (also 7 on some systems)
1Monday
1-5Monday through Friday (weekdays)
6Saturday

Special Characters

Cron expressions support several special characters that give you flexible scheduling control:

CharacterMeaningExampleResult
*Any / Every* * * * *Every minute
,List0 9,17 * * *At 9 AM and 5 PM
-Range0 9-17 * * *Every hour from 9 AM to 5 PM
*/nStep / Every n*/15 * * * *Every 15 minutes
@hourlyMacro: hourly@hourlySame as `0 * * * *`
@dailyMacro: daily midnight@dailySame as `0 0 * * *`
@weeklyMacro: Sunday midnight@weeklySame as `0 0 * * 0`
@monthlyMacro: 1st of month@monthlySame as `0 0 1 * *`
@rebootRun at system startup@rebootRuns once when cron starts

10 Common Cron Schedules

These are the most frequently used cron expressions that cover 90% of real-world use cases:

* * * * *
Every minute
Highest frequency — 1,440 runs/day
*/5 * * * *
Every 5 minutes
Health checks, queue processing
0 * * * *
Every hour (at :00)
Cache clearing, hourly reports
0 0 * * *
Every day at midnight
Daily backups, cleanup tasks
0 9 * * *
Every day at 9 AM
Morning digests, daily reports
0 9 * * 1-5
Weekdays at 9 AM
Business-hours only tasks
0 0 * * 1
Every Monday at midnight
Weekly cleanup, weekly reports
0 9 * * 1
Every Monday at 9 AM
Weekly digests, sprint kick-offs
0 0 1 * *
1st of every month at midnight
Monthly invoices, billing cycles
0 0 1 1 *
January 1st at midnight
Annual tasks, yearly archiving

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:

1

Open your crontab file

crontab -e

This opens your user's crontab in the default editor. Use `sudo crontab -e` for system-wide cron (use with caution).

2

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>&1

Each line is one job: expression + command. The `>> file 2>&1` part redirects both stdout and stderr to a log file.

3

Save and exit

# In nano: Ctrl+X, then Y, then Enter
# In vim: :wq

Cron automatically installs the new schedule — no restart needed.

4

Verify it was saved

crontab -l

Lists 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.sh

Alternatively, 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 -20

Cron 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>&1

The `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.sh

If 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/RHEL

Cron 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:

Put it into practice

Use our free tools to write, test, and validate your cron expressions instantly.