Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Cron in Plain Words

Cron is a daemon that runs commands on a schedule. Every minute it checks its schedule files, called crontabs, and starts each job whose time matches; a cron job is one line that says when to run and what to run.

# minute hour day-of-month month day-of-week command
30 2 * * * /usr/local/bin/backup.sh

This line runs a backup script every day at 02:30, server time.

The Cron Daemon

The scheduler is a background service. Its name depends on the distribution:

# Debian and Ubuntu
systemctl status cron

# AlmaLinux and Rocky Linux
systemctl status crond

If the crontab command is missing on a minimal image, install the package cron on Debian and Ubuntu or cronie on AlmaLinux and Rocky Linux, then enable the service. What a background service is, and how systemd keeps it running, is covered in what is a daemon.

Where Cron Jobs Live

LocationUsed for
crontab -eEach user's own jobs
/etc/crontabSystem jobs
/etc/cron.d/Jobs added by packages or admins
/etc/cron.daily/Scripts run once a day

User crontabs are stored in /var/spool/cron/crontabs on Debian and Ubuntu and in /var/spool/cron on AlmaLinux and Rocky Linux; edit them only with crontab -e, which checks the file when you save. Lines in /etc/crontab and /etc/cron.d/ have one extra field after the time: the user the command runs as. The folders cron.hourly, cron.weekly, and cron.monthly work like cron.daily. To see every job on a server, follow how to list cron jobs.

The Five Time Fields

  • Minute: 0 to 59.
  • Hour: 0 to 23.
  • Day of month: 1 to 31.
  • Month: 1 to 12.
  • Day of week: 0 to 7, where both 0 and 7 mean Sunday.

An asterisk means every value, */5 means every fifth, and shortcuts such as @reboot and @daily replace all five fields. A table of ready schedules is in cron every 5 minutes, and writing a first job step by step is in how to create a cron job.

Why a Job Works in Your Shell but Not in Cron

Cron does not start a login shell, so a job sees a much smaller environment than your terminal:

  • Short PATH: only a few system directories. Use full paths such as /usr/bin/php, or set PATH= at the top of the crontab.
  • Different shell: commands run with /bin/sh, not Bash, unless you set SHELL=/bin/bash.
  • No profile files: variables from .bashrc are not loaded. See environment variables in Linux.
  • Working directory: the job starts in the user's home directory, so use absolute paths for files.
  • The percent sign: an unescaped % ends the command in a crontab line; write \%, for example in a date format.

Longer jobs are easier to keep correct in a script; see how to write a Bash script.

Where the Output Goes

Anything a job prints is sent by e-mail to the crontab's owner, or to the address in a MAILTO= line, but only if the server can deliver mail. Without a mail system, cron on Debian and Ubuntu discards the output and logs only that it did so; cronie on AlmaLinux and Rocky Linux writes it to the system log. Send it to a log file instead:

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

To check that cron started a job, read its log: journalctl -u cron on Debian and Ubuntu, journalctl -u crond on AlmaLinux and Rocky Linux.

Cron vs systemd Timers

Cronsystemd timer
SetupOne lineA timer and a service unit
Missed runsSkipped while offRun after boot if set
OutputYour redirectThe journal
Overlapping runsPossible; use flockOne run at a time

Many packages already use timers, and systemctl list-timers shows them. Cron remains the quicker choice for a single command, while a timer suits jobs that need dependencies, resource limits, or a catch-up run after downtime (Persistent=true in the timer). Both fit into the picture in what is systemd.

Was this answer helpful?
Loading...