Cron Cheatsheet

Crontab syntax, scheduling, special strings & troubleshooting

Linux / Command
Contents
📝

Crontab Syntax

# ┌──────── minute (0-59)
# │ ┌────── hour (0-23)
# │ │ ┌──── day of month (1-31)
# │ │ │ ┌── month (1-12)
# │ │ │ │ ┌ day of week (0-7, 0&7=Sun)
# │ │ │ │ │
# * * * * * command

# Operators
*     # every value
,     # list: 1,3,5
-     # range: 1-5
/     # step: */5 = every 5
📊

Field Values

# Minute:      0-59
# Hour:        0-23
# Day/Month:   1-31
# Month:       1-12 or JAN-DEC
# Day/Week:    0-7 or SUN-SAT (0,7=Sun)

# Examples
30 9 * * *          # 9:30 AM daily
0 */2 * * *         # every 2 hours
0 9 * * 1-5         # 9 AM weekdays
0 0 1 * *           # 1st of every month
*/15 * * * *        # every 15 minutes
0 9,17 * * *        # 9 AM and 5 PM
0 0 * * 0           # midnight Sunday

Special Strings

@reboot     # run once at startup
@yearly     # 0 0 1 1 *   (annually)
@monthly    # 0 0 1 * *
@weekly     # 0 0 * * 0
@daily      # 0 0 * * *   (midnight)
@hourly     # 0 * * * *

# Usage
@reboot /home/user/start.sh
@daily /usr/local/bin/backup.sh
⚙️

Managing Crontab

# Edit crontab
crontab -e                      # edit current user
sudo crontab -e                 # edit root crontab
crontab -e -u username          # edit other user

# List / remove
crontab -l                      # list jobs
crontab -r                      # remove all jobs

# Install from file
crontab mycron.txt

# System-wide
/etc/crontab                    # system crontab
/etc/cron.d/                    # additional configs
/etc/cron.daily/                # daily scripts
/etc/cron.hourly/               # hourly scripts
💡

Examples

# Backup database every night at 2 AM
0 2 * * * /usr/local/bin/db-backup.sh

# Clear temp files weekly (Sunday 3 AM)
0 3 * * 0 find /tmp -mtime +7 -delete

# Health check every 5 min
*/5 * * * * curl -s http://localhost:3000/health

# Log rotation monthly
0 0 1 * * /usr/sbin/logrotate /etc/logrotate.conf

# Send report on weekdays at 6 PM
0 18 * * 1-5 /home/user/send-report.sh

# With output logging
* * * * * /path/script.sh >> /var/log/myjob.log 2>&1

# Discard output
* * * * * /path/script.sh > /dev/null 2>&1
🛡️

Tips & Troubleshooting

# Always use full paths in cron
/usr/bin/python3 /home/user/script.py

# Set PATH in crontab
PATH=/usr/local/bin:/usr/bin:/bin

# Set MAILTO
MAILTO=admin@example.com

# Check cron logs
grep CRON /var/log/syslog
journalctl -u cron

# Cron doesn't load .bashrc
# Source it explicitly if needed:
* * * * * . /home/user/.bashrc; my-command