Cron is a time-based job scheduler in Unix-like operating systems that runs commands, scripts, or programs at specified intervals—daily, weekly, or at any particular minute. It works by reading configuration files called crontabs, which contain lines that define when a command should execute, and the cron daemon (crond) checks these files every minute to trigger the right jobs.
Background: Why Cron Exists
Before cron, system administrators had to manually run repetitive tasks like log rotation, backups, or sending reports. The name “cron” comes from the Greek word chronos, meaning time, and it was introduced in Unix Version 7 in 1979 by Ken Thompson. Cron was designed to automate these routine jobs so they run without human intervention, even when the system is otherwise idle. Over decades, cron has become a standard feature in every Linux distribution, macOS, and BSD system, and it remains the default scheduler for most server-side automation.
How Cron Works: The Daemon and Crontabs
The cron daemon (crond) starts at system boot and runs in the background. Every minute, it wakes up, reads the crontab files, and checks whether any job’s schedule matches the current time. If a match is found, it executes the command as the user who owns that crontab. The daemon does not run continuously; it sleeps and wakes periodically, which is why the smallest granularity is one minute.
Crontab files come in two forms: user crontabs (managed with the crontab command) and system crontabs (usually in /etc/crontab and /etc/cron.d/). User crontabs are stored in /var/spool/cron/crontabs/ (or /var/spool/cron/ on some systems) and are not meant to be edited directly—use crontab -e instead. System crontabs include an extra field for the user who should run the job, allowing system-wide tasks to run as root or other service accounts.
Crontab Syntax: The Five Time Fields
Each crontab line has six fields: minute, hour, day of month, month, day of week, and the command. The first five fields control timing, and the sixth is the command to run. The fields are separated by spaces or tabs.
- Minute (0–59)
- Hour (0–23)
- Day of month (1–31)
- Month (1–12 or names like jan, feb)
- Day of week (0–7, where 0 and 7 are Sunday, or names like sun, mon)
For example, the line 30 2 * * * /usr/bin/backup.sh runs the backup script every day at 2:30 AM. The asterisk (*) means “every possible value” for that field. You can also use lists (1,15), ranges (1-5), steps (*/10), and combinations. A common mistake is thinking that day of month and day of week are both required—if both are restricted, the job runs when either matches, not both. For example, 0 0 1 * mon runs on the first of the month and on every Monday, not only when the first falls on a Monday.
How to Choose: Writing and Managing Crontabs
To create or edit your own crontab, use the command crontab -e. This opens your crontab in the default text editor (often vi or nano). After saving, the crontab is installed and the daemon picks it up automatically. To list your current crontab, use crontab -l; to remove it, crontab -r. For system-wide jobs, edit /etc/crontab or drop a file in /etc/cron.d/ with the correct format.
When scheduling, consider the load on your system. Running heavy jobs at the same minute across many servers can cause spikes. Also, use absolute paths in commands and redirect output to a log file or email, because cron runs with a minimal environment (PATH is often just /usr/bin:/bin). For example, instead of backup.sh, write /home/user/scripts/backup.sh and add >> /var/log/backup.log 2>&1 to capture errors.
Common Problems and How to Avoid Them
Several issues trip up cron users. One is environment variables—your shell profile is not loaded, so commands may fail if they rely on PATH or other settings. Another is permissions: the user running the crontab must have execute permission on the script and write permission on any output files. A third is time zone: cron uses the system’s local time, which can cause confusion if you expect UTC. Also, if the system is asleep or powered off at the scheduled time, the job is skipped (unless you use anacron, which catches up missed jobs).
Debugging tip: test your command manually in the shell first. If it works interactively but not in cron, check the environment. You can add MAILTO=you@example.com at the top of your crontab to receive output by email, or redirect output to a file as mentioned.
Comparison: Cron vs. Anacron vs. systemd Timers
Cron is not the only scheduler. Anacron is a companion tool that runs jobs that were missed because the system was off; it works with daily, weekly, and monthly intervals, but not with minute-level precision. Many modern Linux distributions now use systemd timers, which offer more features: they can run jobs relative to boot, have persistent timers (like anacron), and provide better logging and dependency management. However, cron is still simpler and more portable across Unix-like systems. For most basic tasks, cron is perfectly adequate, but if you need fine-grained control or want to integrate with systemd services, consider timers.
Costs and Safety Considerations
Cron itself is free and built into every Unix-like system, so there is no monetary cost. The real costs are operational: poorly written cron jobs can consume CPU, fill up disks with logs, or send thousands of error emails. Safety is critical when scheduling as root—a mistake can delete files or break services. Always test scripts in a safe environment, use absolute paths, and avoid running destructive commands without proper checks. Also, be aware of security: crontabs can run with elevated privileges, so restrict write access to crontab files and never put sensitive information (like passwords) in command lines, as they are visible in process lists.
Alternatives to Cron
Besides systemd timers and anacron, there are other scheduling tools. For web applications, you might use a job queue like Celery or a cloud scheduler like AWS CloudWatch Events. For simple tasks on a desktop, GUI tools like GNOME Schedule exist. However, cron remains the standard for lightweight, reliable scheduling on any Unix-like system, and learning it is a fundamental skill for system administrators and developers.
Frequently Asked Questions
How do I run a cron job every 5 minutes?
Set the minute field to */5. For example, */5 * * * * /path/to/command runs every 5 minutes. The step syntax */5 means every fifth minute (0, 5, 10, … 55).
Why didn’t my cron job run?
Check the crontab syntax, the command’s absolute path, file permissions, and the system’s time zone. Also, ensure the cron daemon is running (systemctl status cron or ps aux | grep crond). Redirect output to a log to see error messages.
Can I run a job only on weekdays?
Yes, set the day-of-week field to 1-5 (Monday to Friday). For example, 0 9 * * 1-5 /path/to/command runs at 9 AM on weekdays. Remember that day-of-week values 0 and 7 are Sunday.
What is the difference between cron and crontab?
Cron is the daemon (the program that runs in the background), while crontab is both the command to edit your list of jobs and the file that contains the schedule. The crontab file defines what cron executes.
Does cron run jobs if the computer is turned off?
No, cron only runs jobs when the system is on. If a job is missed because the system was off, it is skipped. Anacron is designed to run missed jobs after the system boots, but cron itself does not.
Related
- Four Seasons Hotel Origin: How the Brand Began
- Pegasus Partnership Trust: Companies House Facts
- Dates of the Seasons: Start & End Explained
How pages are sourced and corrected: Sources and editorial policy.