failover.io
  1. Home
  2. Guides
  3. What is a cron job

What is a cron job?

A command your server runs on a schedule. How the line works, how to add one and why it behaves differently from your terminal.

Updated 19 November 20265 min read

A cron job is a command that a Linux or macOS machine runs on a schedule, like every 5 minutes or every night at 3:00. The program that runs them is called cron, and the list of jobs is the crontab. Backups, cleanup scripts, report emails and queue workers are usually cron jobs.

What a cron job looks like

Each job is one line: five time fields, then the command.

30 3 * * * /usr/local/bin/backup.sh

This runs backup.sh every day at 3:30. The five fields are:

FieldAllowed values
Minute0 to 59
Hour0 to 23
Day of month1 to 31
Month1 to 12
Day of week0 to 7 (0 and 7 are Sunday)

A * means every value. */15 in the minute field means every 15 minutes, 1-5 in the day of week field means Monday to Friday, and 0,30 means minute 0 and minute 30. Test any expression with the cron expression tester, or pick a ready one from common cron schedules. Every operator, special string and trap is in crontab syntax.

Shortcuts

ShortcutSame as
@hourly0 * * * *
@daily0 0 * * *
@weekly0 0 * * 0
@monthly0 0 1 * *
@rebootOnce, when the machine starts

Add a cron job

  1. Open your crontab with crontab -e. The first time, it asks which editor to use.
  2. Add a line with the schedule and the full path to the command.
  3. Save and close. Cron picks up the change on its own; there is nothing to restart.
  4. Check it with crontab -l, which lists your cron jobs.

Why a command works in the terminal but not in cron

Cron runs your command in a much smaller environment than your terminal:

DifferenceFix
A short PATH, often just /usr/bin:/binUse full paths: /usr/bin/php, not php. Find them with which php.
No shell profile, so no variables from .bashrcSet the variables in the crontab or at the top of the script.
Starts in the home directoryUse absolute paths, or cd to the right folder first.
Runs with sh, not bashPut bash-only syntax in a script with #!/bin/bash at the top.
Uses the server's time zoneCheck it with timedatectl. Many servers run in UTC.
% means a new lineWrite \%, for example in date +\%F.

Step by step for one language: run a Python script with cron or run a PHP script with cron.

Where the output goes

Cron logs that a job started, not what it printed. The output goes to local mail if the machine can send it, and is dropped otherwise. To keep it, redirect it to a file:

30 3 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

More in where to find cron logs.

Know when a cron job stops running

A cron job that stops running is silent: no error, no email, nothing in the logs. Add a ping at the end of the line, so a heartbeat monitor hears from the job after every successful run and alerts you when a ping is late:

30 3 * * * /usr/local/bin/backup.sh && curl -fsS -m 10 -X POST https://api.failover.io/heartbeat/<your-monitor-id>

The && only sends the ping if the job succeeded. See how to monitor cron jobs.

Frequently asked questions

What is a cron job in simple terms?

A command your server runs on its own on a schedule, for example a backup every night at 3:00. The schedule and the command are one line in a file called the crontab.

What does * * * * * mean in cron?

Every minute. The five stars are minute, hour, day of month, month and day of week, and a star means every value.

How do I see my cron jobs?

Run crontab -l. For other users and system jobs, see how to list cron jobs.

What time zone does cron use?

The server's time zone, unless the crontab sets another with CRON_TZ (supported by cronie on Red Hat based systems). Check the server's zone with timedatectl.

Is cron the same as a systemd timer?

Both run commands on a schedule. A systemd timer is configured in two unit files and logs to the journal; cron is one line in a crontab. Many Linux systems run both.