Introduction to Cron Jobs
Cron is a time-based job scheduler in Unix-like operating systems that enables users to schedule commands or scripts to run automatically at specified intervals. Cron jobs are defined in a crontab (cron table) file, which contains instructions for the cron daemon about when and how often to run specific tasks. Mastering cron syntax allows system administrators, developers, and power users to automate routine tasks, schedule backups, run maintenance scripts, and ensure critical processes execute reliably without manual intervention.
Cron Syntax Fundamentals
Basic Crontab Format
* * * * * command_to_execute
↑ ↑ ↑ ↑ ↑
│ │ │ │ │
│ │ │ │ └── Day of the week (0-6 or SUN-SAT)
│ │ │ └──── Month (1-12 or JAN-DEC)
│ │ └────── Day of the month (1-31)
│ └──────── Hour (0-23)
└────────── Minute (0-59)
Understanding Time Fields
| Field Position | Field Name | Values | Special Characters |
|---|---|---|---|
| 1 | Minute | 0-59 | *, /, -, , |
| 2 | Hour | 0-23 | *, /, -, , |
| 3 | Day of Month | 1-31 | *, /, -, , ? L W |
| 4 | Month | 1-12 or JAN-DEC | *, /, -, , |
| 5 | Day of Week | 0-6 or SUN-SAT | *, /, -, , ? L # |
| 6 | Year (optional) | 1970-2099 | *, /, -, , |
Special Characters Explained
| Character | Name | Description | Example |
|---|---|---|---|
| * | Asterisk | Matches all values in the field | * * * * * = Run every minute |
| / | Slash | Specifies step values | */15 * * * * = Run every 15 minutes |
| – | Hyphen | Defines ranges | 0-5 * * * * = Run in first 6 minutes of every hour |
| , | Comma | Separates multiple values | 1,15,30 * * * * = Run at 1st, 15th, and 30th minute |
| L | Last | Last day of month or week | * * L * * = Run on last day of the month |
| W | Weekday | Nearest weekday to the given day | * * 15W * * = Run on nearest weekday to the 15th |
| # | Hash | Nth day of the month | * * * * 5#3 = Run on third Friday of the month |
| ? | Question mark | No specific value (used in some implementations) | * * * * ? = No specific day of week |
Common Cron Job Examples
Time-Based Schedules
| Schedule | Cron Expression | Description |
|---|---|---|
| Every minute | * * * * * | Run every minute of every hour, every day |
| Every 5 minutes | */5 * * * * | Run every 5th minute (0, 5, 10, 15…) |
| Every hour | 0 * * * * | Run at the start of every hour |
| Every 2 hours | 0 */2 * * * | Run every 2 hours, starting at midnight |
| Twice daily | 0 0,12 * * * | Run at midnight and noon every day |
| Daily at 3:15 AM | 15 3 * * * | Run at 3:15 AM every day |
| Weekdays at 9 AM | 0 9 * * 1-5 | Run at 9 AM Monday through Friday |
| Weekends at 8:30 PM | 30 20 * * 0,6 | Run at 8:30 PM on Saturday and Sunday |
| First day of month | 0 0 1 * * | Run at midnight on the first of every month |
| Every quarter | 0 0 1 1,4,7,10 * | Run at midnight on first day of each quarter |
| Every 6 months | 0 0 1 1,7 * | Run at midnight on January 1 and July 1 |
| Yearly on Dec 31 | 59 23 31 12 * | Run at 11:59 PM on December 31 |
Special Schedules
| Description | Cron Expression | Explanation |
|---|---|---|
| Every reboot | @reboot | Run once at system startup |
| Every hour | @hourly | Same as 0 * * * * |
| Daily | @daily | Same as 0 0 * * * |
| Weekly | @weekly | Same as 0 0 * * 0 |
| Monthly | @monthly | Same as 0 0 1 * * |
| Yearly | @yearly | Same as 0 0 1 1 * |
| Annually | @annually | Same as @yearly |
System-Wide vs. User Crontabs
User Crontabs
- Location: Generally stored in
/var/spool/cron/crontabs/ - Access Command:
crontab -e(edit),crontab -l(list),crontab -r(remove) - Permissions: Runs with the permissions of the specified user
- Format: Does not include a username field
System-Wide Crontabs
- Main System Crontab:
/etc/crontab - Cron Directories:
/etc/cron.d/– System job fragments/etc/cron.daily/– Daily jobs/etc/cron.hourly/– Hourly jobs/etc/cron.monthly/– Monthly jobs/etc/cron.weekly/– Weekly jobs
- Format: Includes a username field to specify which user should run the command
* * * * * username command_to_execute
Crontab Commands and Management
Essential Crontab Commands
| Command | Description | Example |
|---|---|---|
crontab -e | Edit current user’s crontab | crontab -e |
crontab -l | List current user’s crontab entries | crontab -l |
crontab -r | Remove current user’s crontab | crontab -r |
crontab -v | Display the last time you edited your crontab | crontab -v |
crontab file.txt | Install crontab from file | crontab mycron.txt |
crontab -e -u username | Edit another user’s crontab (requires sudo) | sudo crontab -e -u john |
Environment Variables in Crontabs
Cron jobs run with a minimal environment. Add these variables to your crontab for proper execution:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=user@example.com
HOME=/home/username
- SHELL: Defines which shell to use
- PATH: Sets the search path for commands
- MAILTO: Where to send output (empty string means no mail)
- HOME: Sets the home directory
Advanced Cron Techniques
Redirecting Output
| Technique | Example | Description |
|---|---|---|
| Discard all output | 0 * * * * command > /dev/null 2>&1 | Silences all output and errors |
| Log to file | 0 * * * * command > /path/to/log.txt 2>&1 | Redirects output and errors to a log file |
| Append to log | 0 * * * * command >> /path/to/log.txt 2>&1 | Appends output to existing log file |
| Log errors only | 0 * * * * command > /dev/null 2> /path/to/errors.log | Only logs error messages |
| Email output | `0 * * * * command | mail -s “Cron Output” user@example.com` |
Multiple Commands in One Line
# Run multiple commands sequentially
0 5 * * * command1 && command2 && command3
# Run commands regardless of previous command success
0 5 * * * command1; command2; command3
# Run second command only if first fails
0 5 * * * command1 || command2
Using Predefined Schedules
# Create reusable variables
DAILY="0 0 * * *"
WEEKLY="0 0 * * 0"
# Use them in crontab
$DAILY /scripts/daily-backup.sh
$WEEKLY /scripts/weekly-report.sh
Common Cron Job Use Cases
System Maintenance
# Clear temp files daily at 2:30 AM
30 2 * * * find /tmp -type f -atime +7 -delete
# Update package lists weekly on Sunday
0 0 * * 0 apt-get update
# Rotate logs monthly
0 0 1 * * /usr/sbin/logrotate /etc/logrotate.conf
Backups
# Daily database backup at 1 AM
0 1 * * * mysqldump -u user -p'password' database > /backups/db-$(date +\%Y\%m\%d).sql
# Weekly file backup on Saturday at 2 AM
0 2 * * 6 tar -czf /backups/home-$(date +\%Y\%m\%d).tar.gz /home/user
# Monthly cloud sync on the 1st at 3 AM
0 3 1 * * rsync -avz /backups/ user@remote:/backups/
Website & Application Maintenance
# Check website availability every 5 minutes
*/5 * * * * curl -s http://example.com > /dev/null || mail -s "Website Down" admin@example.com
# Clean sessions daily
0 1 * * * find /var/www/sessions -type f -mtime +1 -delete
# Generate sitemap weekly
0 3 * * 0 /var/www/html/generate-sitemap.php
Report Generation
# Generate daily traffic report
0 7 * * * /scripts/traffic-report.sh > /reports/traffic-$(date +\%Y\%m\%d).txt
# Email monthly financial summary
0 8 1 * * /scripts/financial-summary.sh | mail -s "Monthly Financial Report" finance@example.com
Troubleshooting Cron Jobs
Common Issues and Solutions
| Issue | Possible Causes | Solutions |
|---|---|---|
| Job doesn’t run | Syntax errors, path issues, permissions | Check syntax, use absolute paths, ensure proper permissions |
| No output received | Output redirection, missing MAILTO | Check redirection, set MAILTO variable, verify mail setup |
| Incorrect timing | Timezone differences, misunderstanding syntax | Verify system timezone, check cron syntax carefully |
| Command works manually but not in cron | Environment variables, relative paths | Use absolute paths, set environment variables in crontab |
| Sporadic failures | Resource constraints, timing conflicts | Add error handling, stagger job timing, monitor resource usage |
Debugging Tips
Add timestamp output to help track execution:
* * * * * echo "Job ran at $(date)" >> /tmp/cronjob.log && /path/to/actual/scriptTest scripts separately before adding to crontab:
$ sh -x /path/to/script.shCheck cron logs for errors:
$ grep CRON /var/log/syslogVerify cron service is running:
$ systemctl status cronUse a crontab validator to check syntax:
$ crontab -e && echo "Syntax OK"
Cron Security Best Practices
- Use the principle of least privilege (run jobs as non-root users when possible)
- Avoid placing sensitive information like passwords directly in crontab
- Use restricted shell or specific commands to limit what cron can execute
- Implement proper file permissions on scripts run by cron
- Review crontabs regularly to remove unnecessary jobs
- Log and monitor cron job activity
- Consider using secret management tools instead of hardcoded credentials
Cron Alternatives and Extensions
| Tool | Platform | Key Features | Best For |
|---|---|---|---|
| Anacron | Linux | Runs missed jobs after system downtime | Systems that aren’t always on |
| Systemd Timers | Linux | Provides better logging, dependency management | Modern Linux distributions |
| Fcron | Linux | Handles system downtime, supports load balancing | Systems with variable load |
| Cronie | Linux | Standard cron implementation with PAM support | Enterprise Linux distributions |
| Task Scheduler | Windows | Windows equivalent of cron | Windows environments |
| Jenkins | Cross-platform | Advanced scheduling, web interface, plugins | Complex job orchestration |
| Airflow | Cross-platform | Workflow management, dependencies, retries | Data pipeline scheduling |
| Kubernetes CronJobs | Kubernetes | Container-based job scheduling | Containerized environments |
Resources for Further Learning
Documentation
- Man pages:
man cron,man crontab,man 5 crontab - GNU Coreutils documentation
- Linux System Administrator’s Guide
Online Tools
- Crontab.guru – Interactive cron expression editor
- Cronitor – Cron job monitoring service
- Crontab Generator – Visual crontab creator
Books
- “Linux Administration: A Beginner’s Guide” by Wale Soyinka
- “UNIX and Linux System Administration Handbook” by Evi Nemeth et al.
- “Pro Linux System Administration” by James Turnbull
Websites & Blogs
- The Linux Documentation Project (TLDP)
- DigitalOcean Community Tutorials
- LinuxJournal
- ServerFault Q&A
By mastering the cron syntax and best practices outlined in this cheat sheet, you’ll be able to efficiently automate tasks, maintain system health, and ensure critical operations run smoothly and reliably on your Linux systems.
