All guides
Home Glossary Cron job
Term

Cron job

A scheduled task executed automatically at specific intervals or times by the cron daemon in Unix-like operating systems.

A cron job is a scheduled task executed automatically at specific intervals or times by the cron daemon in Unix-like operating systems. The daemon runs continuously as a background process and checks system and user crontab (cron table) files once every minute to trigger commands matching the current system clock. System administrators and developers rely on cron jobs to automate routine maintenance, backups, and data processing without manual intervention.

How it works

Scheduling relies on a configuration line containing five time-and-date fields followed by the command or script path to execute. From left to right, these fields define minutes (0-59), hours (0-23), day of the month (1-31), month (1-12), and day of the week (0-6, where 0 and 7 both represent Sunday). Operators allow flexible scheduling beyond exact timestamps:

  • Asterisks (*): Match every possible value for that field.
  • Commas (,): Separate multiple specific values, such as 0,30 for the top and bottom of an hour.
  • Hyphens (-): Define inclusive ranges, such as 1-5 for Monday through Friday.
  • Slashes (/): Specify step intervals, such as */15 to trigger an action every 15 minutes.

Standard production workloads include database indexing, log rotation to prevent disk space exhaustion, automated security scans, and periodic file synchronization across remote servers. Each user account can maintain its own crontab, and jobs execute with the permissions and ownership of that specific user.

Example

To schedule a database backup script every night at 2:30 AM and record both standard output and errors to a log file, add the following entry using crontab -e:

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

In this configuration, 30 sets the minute, 2 sets the hour, and the three asterisks match every day of the month, every month, and every day of the week. Appending >> /var/log/db-backup.log 2>&1 ensures diagnostic messages are saved to disk rather than discarded or emailed to a local mailbox.

What matters when choosing a server or hosting

Not every hosting environment handles background schedulers the same way. On a Virtual Private Server (VPS) or dedicated server, you have full root access to system-wide directories like /etc/cron.d/ and unrestricted execution intervals. Shared hosting providers frequently restrict crontab access through control panels like cPanel and often enforce minimum intervals—such as preventing jobs from running more frequently than once every five or fifteen minutes—to protect shared CPU resources. In containerized deployments or serverless platforms, traditional daemons are typically replaced by external schedulers, Kubernetes CronJob resources, or managed task queues so jobs persist across container restarts.

Common mistakes

  • Assuming an interactive shell environment: Cron executes with a minimal PATH variable (often just /usr/bin:/bin). Always use absolute file paths for binaries, interpreters, and target files.
  • Ignoring overlapping executions: If a script takes longer to finish than the interval between runs, multiple instances will stack up and exhaust server memory or CPU. Wrap long-running commands with flock to prevent concurrent execution.
  • Forgetting executable permissions or line endings: Scripts fail silently if they lack execute permissions (chmod +x) or contain Windows-style CRLF line endings.
  • Overlooking server time zones: Cron follows the host operating system's local time zone, which is usually UTC on cloud instances rather than your local regional time.

FAQ

What is the difference between cron and crontab?

cron is the background daemon that monitors the clock and executes tasks, while crontab is both the configuration file storing the schedule and the command-line utility used to edit or list those entries.

How can I check if a cron job actually ran?

Inspect the system authentication or cron logs—typically located at /var/log/syslog on Debian/Ubuntu or /var/log/cron on RHEL-based distributions—or redirect the job's output directly into a dedicated log file.

Can cron schedule tasks down to the second?

No. Standard cron has a minimum granularity of one minute. For sub-minute scheduling, administrators either use systemd timers or trigger a wrapper script every minute that uses sleep intervals.

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.