What is a cron job?

On this page

A cron job is a command that the cron scheduler on a Unix-like system runs for you at a time you set, with no one there to start it. People use cron jobs for backups, reports, cleanups and anything else that should happen every hour, day or month.

How does a cron job work?

Cron is a background service that wakes up every minute and looks at a list of scheduled commands. Any command whose schedule matches the current minute is started in its own process. That list is the crontab, short for cron table, and each line in it is one cron job.

crontab
30 2 * * * /usr/local/bin/backup.sh

The first five fields are the schedule and the rest of the line is the command. This job runs backup.sh at 02:30 every day. The fields, their allowed values and the special characters are in the cron syntax cheat sheet.

Where do cron jobs live?

  • Your own crontab. Edit it with crontab -e and read it back with crontab -l. A job in it runs as you.
  • System files. /etc/crontab and the files in /etc/cron.d have an extra column that names the user the job runs as.
  • Directories. On many Linux systems, scripts dropped in /etc/cron.daily, /etc/cron.hourly, /etc/cron.weekly or /etc/cron.monthly run on that rhythm.

Other schedulers run the same idea under another name: systemd timers, Kubernetes CronJobs, GitHub Actions schedules and Windows Task Scheduler. Most of them accept the same five-field schedule. The platform guides cover each.

What are cron jobs used for?

  • Backups of databases and files.
  • Sending reports and digests by email.
  • Deleting old files, logs and expired sessions.
  • Importing data from another system on a schedule.
  • Renewing certificates and refreshing caches.

Browse the cron expression examples for the schedule behind each: every five minutes, every day at 2am, the first of the month and many more.

Why do cron jobs fail without anyone noticing?

A cron job runs with no one watching. If the machine is off, the cron service has stopped, the schedule was deleted or the command fails, nothing breaks loudly: the backup is simply not there when you need it. By default cron mails the output to the owner, and on most machines that mail goes nowhere.

The fix is to have the job report that it ran. Add a request to a monitoring URL after the command, and an alert goes out when the request does not arrive on time:

crontab
30 2 * * * /usr/local/bin/backup.sh && curl -fsS --max-time 10 --retry 5 -o /dev/null https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10

What is cron job monitoring? describes the three ways a job can fail and how one check covers them, and the monitoring checklist helps when a job is not running.

Sources

Questions and answers

What is the difference between cron and a cron job?
Cron is the scheduler program that runs in the background. A cron job is one scheduled command that cron runs for you.
Does a cron job run if the computer is off?
No. Cron runs jobs only while the machine and the cron service are running, and it does not catch up on runs it missed. A job that must run once a day on a laptop needs a different tool, such as anacron.
How do I stop a cron job?
Remove its line from the crontab with crontab -e, or put a # in front of the line to switch it off without deleting it. A job that is running at that moment finishes first.
Know the moment a job goes quiet

Create a check, add one curl to the job, and CronMissed alerts you when a ping is late or reports a failure.

Start monitoring