All posts

Blog

Cron Jobs on Linux: A Practical Guide to Scheduling Tasks

Illustrated cover for the Developers guide "Cron Jobs on Linux: A Practical Guide to Scheduling Tasks"

Backups at 2 a.m. Log cleanup every Sunday. A report emailed on the first of the month. A queue worker poked every five minutes. On Linux servers, the tool behind almost all of these is cron, a scheduler that has been around since early Unix and is still installed on nearly every distribution.

Cron is simple once you understand its syntax and a handful of gotchas. This guide covers both, with examples you can adapt.

What cron actually does

The cron daemon wakes up every minute, checks its schedules, and runs any command whose time has come. Each user can have their own schedule, called a crontab (cron table). There are also system-wide schedules in /etc/crontab and /etc/cron.d/.

Editing your crontab

crontab -e     # edit your crontab
crontab -l     # list it
crontab -r     # remove it entirely (careful: no confirmation)

The first time you run crontab -e, you may be asked to choose an editor. Nano is the friendliest if you are unsure.

To edit another user's crontab as an admin:

sudo crontab -u www-data -e

Running jobs as the user that owns the files (for example, the web server user for a PHP app's scheduler) avoids permission problems later.

Crontab syntax

Each line has five time fields followed by the command:

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

Special characters:

  • * means every value.
  • , lists values: 1,15 means the 1st and 15th.
  • - gives a range: 1-5 means Monday through Friday in the day-of-week field.
  • / sets a step: */10 in the minute field means every 10 minutes.

Examples

# Every 5 minutes
*/5 * * * * /home/deploy/scripts/check-queue.sh

# Every day at 2:30 a.m.
30 2 * * * /home/deploy/scripts/backup.sh

# Every Monday at 9:00 a.m.
0 9 * * 1 /home/deploy/scripts/weekly-report.sh

# Weekdays at 6 p.m.
0 18 * * 1-5 /home/deploy/scripts/end-of-day.sh

# First day of each month at midnight
0 0 1 * * /home/deploy/scripts/monthly-invoice-export.sh

There are also shortcuts like @daily, @weekly, @monthly, and @reboot (run once when the system starts).

A web-based crontab expression explainer is handy for double-checking a schedule before you save it.

The day-of-month and day-of-week trap

If you set both the day-of-month and day-of-week fields to something other than *, cron runs the job when either matches, not both. So 0 9 13 * 5 runs on the 13th of every month and every Friday, not only on Friday the 13th. If you need "both," schedule on one field and test the other inside your script.

The gotchas that break cron jobs

Most "my cron job doesn't run" problems come down to the same few causes.

1. A minimal environment and PATH

Cron does not load your interactive shell profile. Its PATH is minimal, so a command that works in your terminal may fail under cron with "command not found." Fixes:

  • Use absolute paths everywhere: /usr/bin/php, /usr/local/bin/node, /home/deploy/venv/bin/python. Find them with which php.
  • Or set PATH at the top of the crontab:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Environment variables your app needs (API keys, database URLs) also will not be there unless your script loads them, for example by sourcing an env file.

2. Relative paths and the working directory

Cron jobs typically start in the user's home directory. If your script reads ./config.json, cd into the right directory first:

0 * * * * cd /srv/myapp && ./venv/bin/python manage.py clearsessions

3. Percent signs

In a crontab, % is special: it is treated as a newline. A command like date +%Y-%m-%d must escape it as date +\%Y-\%m-\%d, or better, move the logic into a script.

4. Output disappears

By default cron tries to email output to the user, which on most servers goes nowhere useful. Redirect output to a log file so you can see what happened:

30 2 * * * /home/deploy/scripts/backup.sh >> /home/deploy/logs/backup.log 2>&1

2>&1 captures errors as well as normal output. If you want an email when something goes wrong and your server can send mail, set MAILTO=you@example.com at the top of the crontab.

5. The script is not executable

Make sure your script has a shebang line (#!/bin/bash) and execute permissions:

chmod +x /home/deploy/scripts/backup.sh

Preventing overlapping runs

If a job runs every five minutes but sometimes takes seven, two copies may run at once and step on each other. The flock utility prevents that:

*/5 * * * * /usr/bin/flock -n /tmp/sync.lock /home/deploy/scripts/sync.sh

With -n, a new run simply exits if the previous one still holds the lock.

Checking that cron ran

  • On Ubuntu and Debian, cron activity is logged to syslog or the journal: grep CRON /var/log/syslog or journalctl -u cron.
  • On RHEL-based systems, the service is crond and logs go to /var/log/cron.
  • Have important jobs write a timestamp or "success" line to their log, or ping an external monitoring check when they finish, so silence becomes a signal that something broke.

Cron vs systemd timers

Modern distributions also offer systemd timers. They take more setup (a .service file plus a .timer file) but bring real benefits:

  • Output goes to the journal automatically (journalctl -u backup.service).
  • Persistent=true runs a missed job after the machine was off at the scheduled time.
  • Dependencies, resource limits, and randomized delays are built in.
  • systemctl list-timers shows when each job last ran and will run next.

For quick, simple jobs, cron is perfectly fine. For important jobs where logging, catch-up after downtime, and visibility matter, systemd timers are worth the extra lines.

Wrap-up

Cron jobs on Linux come down to five time fields and a command, but reliability comes from the details: absolute paths, an explicit working directory, escaped percent signs, logged output, and locking for long jobs. Get those right and cron will quietly run your backups, cleanups, and reports for years.