Quick Summary: Cron is the built-in Linux task scheduler that runs commands automatically at specified times and intervals. Using crontab (cron table), you can schedule backups, log rotation, system updates, monitoring checks, and any repetitive task. This guide covers crontab syntax, practical scheduling examples, and modern alternatives like systemd timers.
What Is Cron?
Cron is a time-based job scheduler built into every Linux distribution. The cron daemon (crond or cron) runs in the background and checks every minute if any scheduled jobs need to execute. Jobs are defined in crontab files β one per user, plus system-wide crontabs.
Crontab Syntax Explained
Every cron job follows a five-field time specification:
| Field | Values | Description |
|---|---|---|
| Minute | 0-59 | Minute of the hour |
| Hour | 0-23 | Hour of the day (24h format) |
| Day of Month | 1-31 | Day of the month |
| Month | 1-12 | Month of the year |
| Day of Week | 0-7 | Day of the week (0 and 7 = Sunday) |
Special characters: * (any value), , (list), - (range), / (step)
Practical Cron Schedule Examples
| Schedule | Crontab Expression | Use Case |
|---|---|---|
| Every minute | * * * * * | Health monitoring checks |
| Every 5 minutes | */5 * * * * | Resource monitoring |
| Every hour | 0 * * * * | Log rotation |
| Daily at 2 AM | 0 2 * * * | Database backups |
| Weekly Sunday midnight | 0 0 * * 0 | Full system backup |
| Monthly 1st at 3 AM | 0 3 1 * * | Monthly reports |
| Weekdays 9 AM | 0 9 * * 1-5 | Morning status reports |
| Every 15 min, business hours | */15 9-17 * * 1-5 | Business monitoring |
Managing Crontab
crontab -eβ Edit your personal crontabcrontab -lβ List your current cron jobscrontab -rβ Remove all your cron jobs (careful!)sudo crontab -e -u usernameβ Edit another user's crontab
Common Cron Pitfalls and Solutions
| Pitfall | Cause | Solution |
|---|---|---|
| Command works manually but not in cron | Different PATH environment | Use full paths to all commands |
| No output or error messages | Output goes to mail or nowhere | Redirect output: command >> /var/log/job.log 2>&1 |
| Job runs multiple times | No overlap prevention | Use flock: flock -n /tmp/job.lock command |
| Timezone confusion | Cron uses system timezone | Check with timedatectl, set TZ in crontab |
| Permission denied | Script not executable | chmod +x script.sh |
Cron Logging and Monitoring
- View cron logs:
grep CRON /var/log/syslog(Debian) orjournalctl -u crond(RHEL) - Always redirect output to log files for debugging
- Use mail for notifications: cron emails output to the user by default
- Set
MAILTO=admin@example.comat the top of crontab for email alerts
Systemd Timers: The Modern Alternative
Systemd timers offer advantages over cron for complex scheduling:
| Feature | Cron | Systemd Timers |
|---|---|---|
| Logging | Manual (redirect output) | Built-in (journalctl) |
| Dependencies | None | Full dependency support |
| Missed jobs | Skipped | Can run on next boot (Persistent=true) |
| Randomized delay | Not supported | RandomizedDelaySec |
| Resource limits | Not supported | Full cgroup integration |
| Ease of setup | Simple (one line) | More complex (two files) |
Security Best Practices
- Restrict crontab access with
/etc/cron.allowand/etc/cron.deny - Use the principle of least privilege β run cron jobs as non-root when possible
- Audit cron jobs regularly:
for user in $(cut -f1 -d: /etc/passwd); do crontab -l -u $user 2>/dev/null; done - Never store passwords in crontab β use environment files or secret management
Frequently Asked Questions
How do I check if cron is running?
Run systemctl status cron (Debian/Ubuntu) or systemctl status crond (RHEL/AlmaLinux). The service should show "active (running)". If not, start it with systemctl start cron.
Why is my cron job not executing?
The most common reasons are: incorrect PATH (use full paths), missing execute permissions on scripts, syntax errors in the crontab (check with crontab -l), or the cron service not running. Check cron logs for error messages.
Should I use cron or systemd timers?
Use cron for simple, one-line scheduled commands. Use systemd timers when you need logging integration, dependency management, resource limits, or the ability to catch up on missed executions. Both are valid in 2026.
Can I run cron jobs every second?
No, cron's minimum interval is one minute. For sub-minute scheduling, use a loop within a cron job, systemd timers with OnUnitActiveSec=, or a dedicated process.