📋 Cron Time Format
* * * * * command
Minute Hour Day Month Week Command to execute
Minute (0-59)
Execute at the Nth minute of every hour
Hour (0-23)
Execute at the Nth hour of every day
Day (1-31)
Execute on the Nth day of every month
Month (1-12)
Execute in the Nth month of every year
Day of week (0-7)
Execute on the Nth day of every week
(Both 0 and 7 represent Sunday)
🎯 Special Characters
- *Indicates any value, matches all possible values
- ,Indicates a list, e.g., 1,3,5 means 1 o'clock, 3 o'clock, 5 o'clock
- -Indicates a range, e.g., 1-5 means 1 to 5
- /Indicates an interval, e.g., */5 means every 5 units
- ?Used in day and weekday fields, indicates no specific value
⏰ Most Common Scheduled Tasks
🔄 Execute Every Minute
* * * * * /path/to/script.sh
Executes once every minute, commonly used for testing
🕐 Execute Every Hour
0 * * * * /path/to/script.sh
Executes at minute 0 of every hour, e.g., 1:00, 2:00, 3:00
🌅 Execute at 8 AM Every Day
0 8 * * * /path/to/script.sh
Executes at 8:00 AM every day, suitable for daily tasks
🌙 Execute Late Night Every Day
0 2 * * * /path/to/backup.sh
Executes at 2:00 AM every day, suitable for backup tasks
📅 Execute Every Monday
0 9 * * 1 /path/to/weekly-report.sh
Executes at 9:00 AM every Monday, suitable for weekly report generation
📆 Execute on the 1st of Every Month
0 0 1 * * /path/to/monthly-task.sh
Executes in the early morning of the 1st of every month, suitable for monthly tasks
⚡ Execute Every 5 Minutes
*/5 * * * * /path/to/monitor.sh
Executes once every 5 minutes, suitable for monitoring scripts
🔄 Execute Every 30 Minutes
*/30 * * * * /path/to/sync.sh
Executes once every 30 minutes, suitable for sync tasks
🚀 Advanced Scheduling Configuration
💼 Execute Every Hour on Workdays
0 * * * 1-5 /path/to/workday-task.sh
Executes every hour from Monday to Friday, suitable for office automation
🎯 Execute at Specific Times
30 9,14,18 * * * /path/to/reminder.sh
Executes at 9:30, 14:30, 18:30 every day
📈 Every 15 Minutes During Working Hours
*/15 9-17 * * 1-5 /path/to/status-check.sh
Executes every 15 minutes during workdays from 9 AM to 5 PM
🌸 Quarterly Tasks
0 0 1 1,4,7,10 * /path/to/quarterly.sh
Executes on the first day of each quarter (January, April, July, October)
🎉 Annual Tasks
0 0 1 1 * /path/to/yearly-cleanup.sh
Executes annual cleanup on January 1st every year
⏰ Complex Time Combinations
15,45 */2 * * * /path/to/complex-task.sh
Executes at minute 15 and 45 of every 2 hours
🔧 With Output Redirection
0 2 * * * /path/to/backup.sh >> /var/log/backup.log 2>&1
Runs a backup and records logs, including error messages
🌐 Environment Variable Settings
0 3 * * * cd /app && /usr/bin/python3 script.py
Switches to the specified directory and executes a Python script
💡 Practical Application Scenarios
📊 Database Backup
0 2 * * * mysqldump -u root -p database_name > /backup/db_$(date +\%Y\%m\%d).sql
Backs up the MySQL database at 2 AM every day
🧹 Log Cleanup
0 1 * * 0 find /var/log -name "*.log" -mtime +30 -delete
Deletes log files older than 30 days at 1 AM every Sunday
🔄 Git Auto Sync
*/10 * * * * cd /path/to/repo && git pull origin main
Automatically pulls Git repository updates every 10 minutes
📧 System Report Email
0 8 * * 1 df -h | mail -s "Weekly Disk Usage" admin@example.com
Sends a disk usage email every Monday
🌡️ System Monitoring
*/5 * * * * top -bn1 | grep "Cpu(s)" >> /var/log/cpu_usage.log
Records CPU usage every 5 minutes
🔒 SSL Certificate Check
0 9 * * * openssl x509 -in /etc/ssl/cert.pem -noout -checkend 2592000
Checks daily whether the SSL certificate is about to expire
📱 API Health Check
*/2 * * * * curl -f http://localhost:8080/health || echo "API Down" | mail -s "Alert" admin@example.com
Checks API status every 2 minutes, sends an email on anomalies
🔄 Docker Container Restart
0 4 * * * docker restart $(docker ps -q --filter "status=exited")
Restarts stopped Docker containers at 4 AM every day
💎 Expert Tips and Best Practices
📝 Cron Management Commands
crontab -e # Edit the current user's crontab
crontab -l # View the current crontab
crontab -r # Delete the current crontab
crontab -l # View the current crontab
crontab -r # Delete the current crontab
Basic crontab management commands
👤 User-Level Management
sudo crontab -u username -e # Edit a specified user's crontab
sudo crontab -u username -l # View a specified user's crontab
sudo crontab -u username -l # View a specified user's crontab
Manage other users' scheduled tasks
📊 Logging Best Practices
0 2 * * * /path/to/script.sh >> /var/log/cron_script.log 2>&1
Redirect both standard output and errors to a log file
🔧 Environment Variable Settings
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0 2 * * * /path/to/script.sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0 2 * * * /path/to/script.sh
Set environment variables in crontab
⚠️ Prevent Task Overlap
0 * * * * flock -n /tmp/script.lock -c '/path/to/script.sh'
Use file locks to prevent scripts from executing repeatedly
🔍 Debugging Cron Tasks
# Check cron service status
systemctl status cron
# View cron logs
tail -f /var/log/cron
systemctl status cron
# View cron logs
tail -f /var/log/cron
Common methods for debugging scheduled tasks
📊 Special Time Expressions
@reboot /path/to/startup.sh # Execute at system startup
@yearly /path/to/yearly.sh # Execute yearly
@monthly /path/to/monthly.sh # Execute monthly
@weekly /path/to/weekly.sh # Execute weekly
@daily /path/to/daily.sh # Execute daily
@hourly /path/to/hourly.sh # Execute hourly
@yearly /path/to/yearly.sh # Execute yearly
@monthly /path/to/monthly.sh # Execute monthly
@weekly /path/to/weekly.sh # Execute weekly
@daily /path/to/daily.sh # Execute daily
@hourly /path/to/hourly.sh # Execute hourly
Use special strings to simplify time expressions
⚡ Performance Optimization Tips
# Avoid executing when the system is busy
5 2 * * * /path/to/heavy-task.sh
# Stagger execution times to avoid running multiple tasks simultaneously
7 3 * * * /path/to/task1.sh
13 3 * * * /path/to/task2.sh
5 2 * * * /path/to/heavy-task.sh
# Stagger execution times to avoid running multiple tasks simultaneously
7 3 * * * /path/to/task1.sh
13 3 * * * /path/to/task2.sh
Reasonably schedule task execution times