Cron Job Examples: The Complete Linux Scheduling Guide

6 min read
Beginner Linux Cron Automation Sysadmin

Cron is the built-in task scheduler on every Linux and Unix system. If you need something to run automatically — a backup at 2 AM, a log cleanup every Sunday, an SSL certificate renewal every month — cron is how you do it.

This guide is not theory. It is a collection of practical, copy-paste-ready cron examples that cover the most common tasks sysadmins and developers need. Every example includes the cron expression, the command, and an explanation of when and why you would use it.

Crontab Basics

The Five Fields

A cron expression has five fields separated by spaces:

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

Special Characters

Character Meaning Example
* Every value * = every minute
, List 1,15 = 1st and 15th
- Range 1-5 = Monday through Friday
/ Step */5 = every 5 units

Managing Your Crontab

crontab -e          # Edit your crontab
crontab -l          # List current cron jobs
crontab -r          # Remove all cron jobs (careful!)
sudo crontab -e     # Edit root's crontab

Time-Based Examples

Run Every Minute

* * * * * /path/to/script.sh

Use for: health checks, queue workers, real-time monitoring.

Run Every 5 Minutes

*/5 * * * * /path/to/script.sh

Use for: checking service status, syncing data, polling an API.

Run Every 15 Minutes

*/15 * * * * /path/to/script.sh

Use for: less frequent monitoring, cache warming.

Run Every Hour (on the Hour)

0 * * * * /path/to/script.sh

Use for: hourly reports, log aggregation.

Run Every 6 Hours

0 */6 * * * /path/to/script.sh

Use for: periodic sync jobs, DNS updates.

Run at Midnight Every Day

0 0 * * * /path/to/script.sh

Use for: daily reports, database cleanup, log rotation.

Run at 3:30 AM Every Day

30 3 * * * /path/to/script.sh

Use for: backups (off-peak hours), maintenance tasks.

Run Every Weekday at 9 AM

0 9 * * 1-5 /path/to/script.sh

Use for: business-hours tasks, weekday reports.

Run Every Monday at 6 AM

0 6 * * 1 /path/to/script.sh

Use for: weekly reports, weekly cleanup.

Run on the 1st of Every Month

0 0 1 * * /path/to/script.sh

Use for: monthly reports, billing tasks, certificate checks.

Run Quarterly (Jan, Apr, Jul, Oct)

0 0 1 1,4,7,10 * /path/to/script.sh

Run at Reboot

@reboot /path/to/script.sh

Use for: starting services, initializing environment.

Real-World Task Examples

Database Backup (MySQL)

# Daily backup at 2 AM, keep 7 days
0 2 * * * mysqldump -u root --all-databases | gzip > /backups/mysql/db-$(date +\%Y\%m\%d).sql.gz && find /backups/mysql/ -name "*.sql.gz" -mtime +7 -delete

Database Backup (PostgreSQL)

# Daily backup at 2 AM
0 2 * * * pg_dumpall -U postgres | gzip > /backups/postgres/all-$(date +\%Y\%m\%d).sql.gz && find /backups/postgres/ -name "*.sql.gz" -mtime +7 -delete

File/Directory Backup with rsync

# Daily at 3 AM — sync /var/www to backup server
0 3 * * * rsync -avz --delete /var/www/ backup-server:/backups/www/

SSL Certificate Renewal (Let's Encrypt)

# Check twice daily (certbot only renews if needed)
0 3,15 * * * certbot renew --quiet --post-hook "systemctl reload nginx"

Log Cleanup

# Delete logs older than 30 days, every Sunday at 4 AM
0 4 * * 0 find /var/log/app/ -name "*.log" -mtime +30 -delete

Disk Space Alert

# Check disk space every hour, email if above 90%
0 * * * * df -h / | awk 'NR==2{gsub(/%/,"",$5); if($5>90) print "DISK ALERT: "$5"% used"}' | mail -s "Disk Alert" [email protected]

Docker Cleanup

# Remove unused Docker images and containers weekly
0 5 * * 0 docker system prune -af --volumes > /dev/null 2>&1

System Update Check

# Check for updates daily, log results
0 6 * * * apt-get update -qq && apt-get --just-print upgrade 2>&1 | grep "^Inst" >> /var/log/pending-updates.log

Website Uptime Monitor

# Check every 5 minutes, log failures
*/5 * * * * curl -sf https://www.samnet.dev > /dev/null || echo "$(date): Site DOWN" >> /var/log/uptime.log

Nginx Access Log Rotation

# Rotate and compress Nginx logs weekly
0 0 * * 0 mv /var/log/nginx/access.log /var/log/nginx/access-$(date +\%Y\%m\%d).log && kill -USR1 $(cat /var/run/nginx.pid) && gzip /var/log/nginx/access-$(date +\%Y\%m\%d).log

Git Auto-Pull (Deploy)

# Pull latest code every 5 minutes (simple auto-deploy)
*/5 * * * * cd /var/www/myapp && git pull origin main > /dev/null 2>&1

Clear Temporary Files

# Delete temp files older than 24 hours, daily at 4 AM
0 4 * * * find /tmp -type f -mtime +1 -delete 2>/dev/null

Cron Best Practices

Always Redirect Output

Cron emails you the output of every command (if mail is configured). This can fill up mailboxes or cause issues. Redirect to a log file or /dev/null:

# Log output
0 2 * * * /backup.sh >> /var/log/backup.log 2>&1

# Suppress all output
0 2 * * * /backup.sh > /dev/null 2>&1

Use Full Paths

Cron runs with a minimal environment. Always use absolute paths for commands and files:

# Bad (might not find the command)
0 * * * * python3 script.py

# Good
0 * * * * /usr/bin/python3 /opt/scripts/script.py

Escape Percent Signs

In crontab, % is a special character (newline). Escape it with \%:

# Bad
0 2 * * * echo "$(date +%Y%m%d)"

# Good
0 2 * * * echo "$(date +\%Y\%m\%d)"

Test Before Scheduling

Always run your command manually first to make sure it works:

# Test the command
/usr/bin/python3 /opt/scripts/backup.py

# Then add to cron
crontab -e

Add Comments

# Daily database backup at 2 AM
0 2 * * * /opt/scripts/db-backup.sh >> /var/log/db-backup.log 2>&1

# SSL cert renewal check (twice daily)
0 3,15 * * * /usr/bin/certbot renew --quiet

Use a Lock File for Long Jobs

If a job takes longer than its interval, you can end up with overlapping runs:

# Prevent overlapping runs with flock
*/5 * * * * /usr/bin/flock -n /tmp/myjob.lock /opt/scripts/long-task.sh

Troubleshooting Cron

Job Not Running?

  1. Check cron is running: systemctl status cron
  2. Check syntax: crontab -l to verify
  3. Check logs: grep CRON /var/log/syslog
  4. Check permissions: Is the script executable? (chmod +x script.sh)
  5. Check paths: Use absolute paths for everything
  6. Check environment: Cron does not load your shell profile — set PATH at the top of crontab if needed

Setting PATH in Crontab

PATH=/usr/local/bin:/usr/bin:/bin
SHELL=/bin/bash

0 2 * * * /opt/scripts/backup.sh

Build Your Cron Expression

Use our free Cron Expression Generator to visually build cron schedules, see human-readable descriptions, and preview the next 5 run times. No more guessing if 0 /6 * 1-5 means what you think it means.

See Also