Automation

Cron Jobs Explained: Scheduling Tasks on Linux

By ScriptTrip Editorial Team · · 5 min read

Cron is the classic Unix tool for running commands at set times without anyone being logged in. Backups at night, log cleanup every week, a report on the first of the month: all of these are typical cron jobs. It has been around for decades, it is installed on nearly every Linux system, and it is still one of the most practical automation tools you can learn.

This article explains how cron reads its configuration, how to write a schedule, and how to avoid the mistakes that make a job silently fail.

How cron works

A background service called the cron daemon wakes up every minute and checks a set of tables, each called a crontab (short for cron table). Any entry whose time pattern matches the current minute gets started. Each user can have a personal crontab, and the system has extra files for administrative jobs.

To edit your own table, run:

crontab -e

To list what is currently scheduled, use crontab -l. To remove your whole table, crontab -r exists, but it deletes everything without asking, so be careful with that one.

Reading the five time fields

Each line in a crontab has five time fields followed by the command to run.

* * * * * command
| | | | |
| | | | +-- day of week (0 to 7, where 0 and 7 are Sunday)
| | | +---- month (1 to 12)
| | +------ day of month (1 to 31)
| +-------- hour (0 to 23)
+---------- minute (0 to 59)

An asterisk means "every value." A comma gives a list, a dash gives a range, and a slash gives a step. Some examples:

  • 0 2 * * * runs at 02:00 every day.
  • */15 * * * * runs every fifteen minutes.
  • 30 6 * * 1-5 runs at 06:30 on weekdays.
  • 0 0 1 * * runs at midnight on the first day of each month.
  • 0 9,17 * * * runs at 09:00 and again at 17:00.

If you are unsure whether a pattern means what you think, write it out in words before saving it. A mistaken field is the most common reason a job runs at the wrong time or never.

A complete example

Suppose you have a script that compresses a log folder. Add a line like this:

0 3 * * 0 /home/sam/bin/archive-logs.sh >> /home/sam/archive.log 2>&1

This runs the script at 03:00 every Sunday. The >> appends normal output to a file, and 2>&1 sends error messages to the same place. Without that redirection, cron tries to email the output to the local user, and on many machines no mail system is set up, so the output simply vanishes.

The environment is not your shell

The biggest surprise for newcomers is that cron runs jobs in a very small environment. It does not load your usual shell startup files, so your PATH is short, and variables you rely on may be missing. A command that works in your terminal can fail under cron for exactly this reason.

Three habits prevent most problems:

  1. Use full paths to programs and files, such as /usr/bin/python3 rather than python3.
  2. Set what you need at the top of the crontab, for example PATH=/usr/local/bin:/usr/bin:/bin.
  3. Put complicated logic inside a script, and let the cron line only call that script.

You can also set SHELL=/bin/bash at the top of the file if your commands need Bash features, since cron defaults to the plain sh shell.

Special shortcuts

Many cron versions accept readable shortcuts in place of the five fields:

  • @reboot runs once when the system starts.
  • @daily runs once a day, at midnight.
  • @weekly runs once a week.
  • @hourly runs at the start of every hour.

These are easier to read, but they give you less control over exact times, so use them for simple cases only.

System crontabs and drop-in folders

Besides personal tables, Linux systems usually have a file at /etc/crontab and a folder at /etc/cron.d. Entries in those places include an extra field naming the user who should run the command. Many distributions also provide folders such as /etc/cron.daily, where you can simply drop an executable script and let the system run it once a day. These are tidy choices for server administration, because the jobs live in files you can track in version control.

Testing and debugging

Cron gives little feedback, so build your own checks.

  • Run the exact command from a clean shell first. Use env -i /bin/sh -c 'your command' to see how it behaves with an empty environment.
  • Temporarily schedule the job for the next minute or two, and confirm that it fires.
  • Check the system log. On many systems, grep CRON /var/log/syslog or the journal shows each run.
  • Make jobs safe to repeat. If a job runs twice, or overlaps with the previous run, it should not cause damage. A lock file or the flock utility can stop overlapping runs.

When cron is not the right tool

Cron assumes the machine is awake at the scheduled time. A laptop that is asleep at 03:00 simply misses the job. For desktops and laptops, systemd timers with the persistent option, or a tool that catches up on missed runs, may suit you better. For jobs that depend on each other, a proper workflow tool is a wiser choice than chaining many cron lines.

Frequently Asked Questions

How do I run a cron job every five minutes?

Use a step value in the minute field: */5 * * * *. This matches minutes 0, 5, 10 and so on, every hour of every day.

Why did my cron job not run?

Common causes are a wrong time pattern, a missing execute permission on the script, a short PATH that cannot find the command, or an error that was never recorded because output was not redirected. Check the system log first, then run the command by hand with a minimal environment.

Can I run cron jobs at a time zone other than the server's?

Cron normally uses the system time zone. Some versions support a CRON_TZ variable, but support varies, so check your documentation. A safer approach is to calculate the equivalent local time yourself, or to keep servers on UTC and plan around that.

Related reading