All guides
Term

cron

cron is a time-based job scheduler in Unix-like operating systems that runs background processes at specified intervals.

cron is a time-based job scheduler in Unix-like operating systems that runs background processes at specified intervals. System administrators and developers use it to automate repetitive tasks—such as backups, log rotation, and queue processing—without manual intervention. Schedules are defined in configuration files called crontabs, which pair a five-field time expression with a shell command or script.

How it works

The background daemon crond wakes up once every minute to inspect system-wide schedules in /etc/crontab, drop-in directories like /etc/cron.d/, and user-specific tables stored in /var/spool/cron/crontabs (or /var/spool/cron depending on the distribution). Whenever an entry's schedule matches the current system clock, the daemon spawns a subshell to execute the command.

Each standard crontab line follows a strict five-field syntax before the command: minute hour day-of-month month day-of-week. Standard values range from 0-59 for minutes, 0-23 for hours, 1-31 for days, 1-12 for months, and 0-7 for days of the week (where both 0 and 7 represent Sunday). Wildcards (*), step values (*/15), ranges (1-5), and shorthand keywords like @hourly or @reboot provide flexible scheduling. Any standard output or error text generated by a job is mailed to the local user account unless explicitly redirected to a file or /dev/null.

Example

To edit your user schedule, run crontab -e. Below is a practical configuration snippet demonstrating environment definitions, certificate renewal, and automated database backups with output logging:

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# Renew SSL certificates every day at 4:00 AM
0 4 * * * /usr/bin/certbot renew --quiet

# Back up a PostgreSQL database every Sunday at 2:30 AM and log errors
30 2 * * 0 /usr/local/bin/pg_backup.sh >> /var/log/pg_backup.log 2>&1

# Run a custom sync script at the start of every hour
@hourly /usr/local/bin/sync_data.sh

What matters when choosing a server or hosting

  • Access level: Shared hosting plans often restrict direct shell access and limit how frequently jobs can run, forcing you to use a web control panel interface. Virtual private servers (VPS) and dedicated instances grant full root and user crontab control.
  • Container and serverless architecture: Ephemeral containers typically run a single foreground process rather than a background crond service. In container orchestrators or serverless environments, platform-native schedulers (such as Kubernetes CronJobs or cloud event timers) are usually more reliable than installing a daemon inside an image.
  • System timezone and NTP: Because cron triggers strictly off the host's local clock, verify that your server uses Network Time Protocol (NTP) synchronization and is set to a predictable timezone, typically UTC, to avoid unexpected shifts during daylight saving transitions.

Common mistakes

  • Assuming an interactive shell environment: cron executes jobs with a minimal set of environment variables. Scripts that work manually often fail in cron because $PATH lacks directories like /usr/local/bin; always use absolute paths to binaries and files.
  • Unescaped percent signs: Inside a crontab entry, the % character is treated as a newline unless escaped with a backslash (\%). Commands using date +%F will break silently if not properly escaped.
  • Overlapping job executions: cron does not check whether a previous instance of a task is still running before starting a new one. Long-running scripts should implement file locking (such as flock) to prevent resource exhaustion.

FAQ

What is the difference between cron and systemd timers?

cron is a traditional, lightweight scheduler configured via simple text tables. systemd timers offer deeper Linux integration, including dependency management, randomized delays, built-in logging via journald, and the ability to run missed jobs after a system wakes from downtime.

How can I check if my cron job actually ran?

Inspect the system authentication or cron log—typically located at /var/log/syslog on Debian/Ubuntu or /var/log/cron on RHEL-based distributions—to verify execution timestamps, and redirect script output to a dedicated log file to capture errors.

Can cron schedule tasks down to the second?

No. The finest granularity supported by standard crontab syntax is one minute. Sub-minute intervals require either a background worker daemon, a custom sleep loop inside a script, or a systemd timer configured with millisecond or second accuracy.

All glossary terms

Glossary

Related terms

Full glossary

New guides every week.

A digest of the best pieces on servers, VPN and self-hosted software. No ads, no spam.

1 email a weekOne-click unsubscribeRU · EN
Only slipjar.app articles, nothing else.