GameParticles
Developer

What Is a Cron Job? How Cron Works, Syntax and Examples

What is a cron job? A command that Linux runs automatically on a schedule. Learn how cron works, how to edit your crontab, common schedules and the gotchas.

Updated

A cron job is a command or script that a Unix-like system runs automatically on a repeating schedule, such as every five minutes or at 03:00 each night. The schedule and the command sit together on one line of a file called a crontab, and a background service named cron reads those files and starts each job when its time comes. It is the standard way to automate backups, cleanups, reports and other routine work on Linux and macOS.

How cron runs

Cron is a daemon, a process that starts at boot and stays running. Once a minute it wakes up, compares the current time with every entry in every crontab, and launches the commands whose schedule matches. It then goes back to sleep until the next minute. That is why one minute is the smallest interval cron can express.

Each job runs as the user who owns the crontab, in a new non-interactive shell, with no terminal attached. Cron does not wait for a job to finish and keeps no record of whether an earlier run succeeded, overlapped or was missed.

Anatomy of a crontab line

A crontab entry is five schedule fields followed by the command:

30 2 * * 1 /home/ada/bin/backup.sh

FieldAllowed valuesIn the example
Minute0-5930
Hour0-232
Day of month1-31* (any)
Month1-12* (any)
Day of week0-6, Sunday is 01 (Monday)

So the line means "run backup.sh at 02:30 every Monday". Four operators cover nearly every schedule: * for every value, , for a list, - for a range and / for a step, as in */5. The cron expression cheat sheet goes through the syntax in depth, including the rule that day of month and day of week combine with OR.

Editing your crontab

You do not edit crontab files directly. The crontab command manages them:

  • crontab -e opens your crontab in an editor. When you save and quit, cron installs the new version, with no restart needed.
  • crontab -l prints your current crontab.
  • crontab -r deletes your entire crontab without asking. The e and r keys are neighbors, so keep a copy: crontab -l > crontab.bak.

Lines starting with # are comments.

Besides per-user crontabs there are system ones. /etc/crontab and files in /etc/cron.d use the same format with one extra field, the user to run as, between the schedule and the command. Many distributions also provide /etc/cron.hourly, cron.daily, cron.weekly and cron.monthly directories: drop an executable script in and it runs at that frequency.

Common schedules

ScheduleExpression
Every minute* * * * *
Every 5 minutes*/5 * * * *
Every 15 minutes*/15 * * * *
Every hour, on the hour0 * * * *
Every day at midnight0 0 * * *
Every day at 03:3030 3 * * *
Weekdays at 09:000 9 * * 1-5
Every Sunday at 04:000 4 * * 0
First day of the month0 0 1 * *
At startup@reboot

A complete entry for "every 5 minutes" looks like */5 * * * * /usr/bin/python3 /opt/app/sync.py >> /var/log/sync.log 2>&1. Most Linux crons also accept the shortcuts @hourly, @daily, @weekly, @monthly and @yearly in place of the five fields.

Before saving a schedule, check it. The Cron Expression Builder lets you set each of the five fields or start from a preset, describes the result in plain English, and lists the next five run times. Those times are calculated in your own computer's time zone, so adjust if the server uses a different one.

Gotchas

Most "my cron job does nothing" problems come from the gap between your terminal and cron's bare environment.

A minimal environment

Cron does not load your .bashrc or .profile. The PATH is typically just /usr/bin:/bin, and variables you rely on, such as API keys or NODE_ENV, are absent. A script that works when you type it can fail under cron with "command not found".

Fixes: use absolute paths for every program and file, set variables at the top of the crontab with lines like PATH=/usr/local/bin:/usr/bin:/bin, or have the job call a wrapper script that sets up what it needs. The default shell is /bin/sh, not bash; add SHELL=/bin/bash if the command uses bash features.

Time zone

Cron uses the system's local time zone. Many servers and containers are set to UTC, so 0 9 * * * fires at 09:00 UTC, not 09:00 where you are. Run date on the machine to see what it thinks the time is. Some cron implementations, including cronie on Red Hat based systems, accept a CRON_TZ=Europe/Berlin line to set the zone for the entries that follow.

In zones with daylight saving time, a job scheduled inside the hour that is skipped or repeated can be missed or run twice, depending on the implementation. Scheduling in UTC, or outside the 01:00 to 03:00 window, avoids it.

Output goes nowhere obvious

Cron tries to email whatever a job prints to the crontab's owner. On machines without a mail setup, that output is lost. Redirect it yourself: >> /var/log/myjob.log 2>&1 appends both normal output and errors to a file. MAILTO="" at the top of the crontab turns the email attempt off.

The percent sign

In a crontab command, an unescaped % is treated as a line break, and everything after it is sent to the command as input. date +%F therefore breaks. Write date +\%F, or move the command into a script.

Overlapping and missed runs

If a job takes six minutes and runs every five, cron starts a second copy while the first is still going. Wrap it with flock -n /tmp/myjob.lock so a new run exits when the lock is held. If the machine is off at the scheduled time, the run is skipped; anacron or a systemd timer with Persistent=true can catch up after boot.

To debug, make the job log everything, then temporarily schedule it for every minute. On Debian and Ubuntu, grep CRON /var/log/syslog shows whether cron started it; on systemd systems, try journalctl -u cron or journalctl -u crond.

Cron outside Linux servers

The five-field expression has spread well beyond the original daemon. GitHub Actions schedule triggers, Kubernetes CronJobs and most hosted schedulers use it, usually in UTC.

When a job records when it ran, it often stores a Unix timestamp. The Unix timestamp converter turns those values into readable dates so you can check that runs happened when expected.

Try it

Build your schedule in the Cron Expression Builder, confirm the English summary and the next five run times, then paste the expression into crontab -e in front of your command with absolute paths and a log redirect.

Tools in this guide

Frequently asked questions

How do I run a crontab job every 5 minutes?

Put */5 in the minute field and an asterisk in the other four: */5 * * * * /path/to/command. The job runs at minutes 0, 5, 10 and so on of every hour. Add it with crontab -e and save the file.

What is the cron syntax?

Five fields separated by spaces, followed by the command: minute (0-59), hour (0-23), day of month (1-31), month (1-12) and day of week (0-6, Sunday is 0). An asterisk means every value, a comma lists values, a hyphen is a range and a slash is a step.

Where are cron jobs stored?

Per-user crontabs live under /var/spool/cron, in a crontabs subdirectory on Debian and Ubuntu, and should be edited only with crontab -e. System-wide jobs are in /etc/crontab and /etc/cron.d, and scripts in /etc/cron.daily and similar folders run on fixed schedules.

Why is my cron job not running?

The usual causes are a command that relies on your interactive PATH, a script without execute permission, an unescaped percent sign, or a schedule in a different time zone than you assumed. Redirect the job's output to a log file to see the actual error.

More developer guides

JSON Formatter
Developer · Format, validate and minify JSON with syntax highlighting.
↵
Base64 Encode / Decode
Developer · Encode text to Base64 or decode Base64 back to plain text.
↵
URL Encoder / Decoder
Developer · Encode or decode URL components and query strings.
↵
Hash Generator
Developer · Generate MD5, SHA-1, SHA-256 and SHA-512 hashes.
↵
UUID Generator
Developer · Generate UUIDs v4 (random) in bulk with one click.
↵
Regex Tester
Developer · Test regular expressions against text with live match highlighting.
↵
Word Counter
Text · Count words, characters, sentences, paragraphs and reading time.
↵
Case Converter
Text · Convert text between UPPER, lower, Title, camelCase, snake_case and more.
↵
List Sorter
Text · Sort, reverse, deduplicate and shuffle lists of text.
↵
Markdown Preview
Text · Write Markdown and see a live rendered preview side by side.
↵
Percentage Calculator
Numbers · Calculate percentages, increases, decreases and what % one number is of another.
↵
Color Converter
Colors · Convert colors between HEX, RGB, HSL and HSV instantly.
↵
Snake
Arcade · Eat food, grow longer, don't hit the walls. A timeless classic.
↵
2048
Puzzle · Slide tiles and combine them to reach the legendary 2048 tile.
↵
Word Grid
Puzzle · Guess the hidden five letter word in six tries using color clues.
↵
Sudoku
Puzzle · Fill the 9x9 grid with logic. Four difficulties from easy to expert.
↵
Minesweeper
Strategy · Use number clues to clear the minefield. Classic logic, three levels.
↵
Breakout
Arcade · Smash every brick with paddle and ball. Power-ups, combos, endless levels.
↵
Typing Practice
Casual · Build your WPM and accuracy with timed drills from 30 seconds to 5 minutes. Find your weak keys.
↵
TypeRunner
Arcade · Multiplayer typing race. Race friends by link, beat computer runners, or chase your own ghost.
↵
Typing Blocks
Arcade · Type each word before its wooden block lands. Miss, and the pile climbs toward the line.
↵
Bubble Shooter
Arcade · Aim, bank shots off the walls, and pop groups of three or more bubbles.
↵
Road Rush
Arcade · Race seven rivals through traffic and punch your way to the front.
↵