Guides
Cron Jobs Explained: Schedule Backups and Log Cleanup
By Ravindra Bagale, DevOps trainer · LinkedIn · Published: · Last updated:
Cron schedules backups and log cleanup on Linux. Learn fields, redirects and PATH fixes used on EC2 and on-prem hosts.
Friends, cron is boring until backups miss a night. Chala mitrano — log every run.
मित्रांनो, cron कंटाळवाणे वाटते जोपर्यंत backup रात्र चुकत नाही. Chala mitrano — प्रत्येक run log करा.
मित्रों, cron उबाऊ लगता है जब तक backup रात न चूके। Chala mitrano — हर run log करें।
Quick answer
Hold this vertical picture:
- Learn five crontab fields.
- Script does the work.
- Redirect logs.
- Fix PATH and permissions.
- Verify in syslog or log file.
Language: English · मराठी · हिंदी
Cron job path
Why cron still matters
Cloud schedulers exist, but every Amazon EC2 and on-prem box still needs host-local cleanup. Missed backups cost INR and dollars alike.
Why: Unattended work must be reliable.
What: crontab fields, backup and cleanup jobs, failure fixes.
How: Build a script, schedule it, prove the log.
Concept 1: Crontab fields
Why: Wrong hour means wrong night.
What: min hour dom mon dow + command.
How: Memorize with one daily example.
Example 1: Crontab fields
Type this:
printf '%s\n' 'min hour dom mon dow command' '0 2 * * * /home/ec2-user/bin/backup.sh'
You will see:
min hour dom mon dow command
0 2 * * * /home/ec2-user/bin/backup.sh
What it means: 0 2 * * * means 02:00 every day.
Example 2: List crontab
Type this:
crontab -l 2>/dev/null || echo no-crontab-yet
You will see:
What it means: Empty until you install one.
Example 3: Five field quiz
Type this:
python3 -c "print('*/5 * * * * -> every 5 minutes')"
You will see:
*/5 * * * * -> every 5 minutes
What it means: Star means every unit.
Concept 2: Backup and cleanup scripts
Why: Cron only launches; the script does the work.
What: tar backup; find for old logs.
How: Dry-run deletes with -print.
Example 4: Backup script stub
Type this:
printf '%s\n' '#!/usr/bin/env bash' 'set -euo pipefail' 'tar -czf /tmp/app-$(date +%F).tgz /var/www/app' > /tmp/backup.sh
chmod 700 /tmp/backup.sh
head -3 /tmp/backup.sh
You will see:
#!/usr/bin/env bash
set -euo pipefail
tar -czf /tmp/app-$(date +%F).tgz /var/www/app
What it means: Date-stamped tarball pattern.
Example 5: Log cleanup find
Type this:
printf '%s\n' 'find /var/log/myapp -name "*.log" -mtime +7 -delete'
You will see:
find /var/log/myapp -name "*.log" -mtime +7 -delete
What it means: Delete logs older than 7 days — test with -print first.
Example 6: Dry-run delete
Type this:
printf '%s\n' 'find /tmp/demo-logs -name "*.log" -mtime +7 -print'
You will see:
find /tmp/demo-logs -name "*.log" -mtime +7 -print
What it means: Always print before delete on production.
Concept 3: Redirects and MAILTO
Why: Silent cron jobs hide failure.
What: >> log 2>&1; optional MAILTO=.
How: Always capture stderr.
Example 7: Redirect pattern
Type this:
printf '%s\n' '0 2 * * * /home/ec2-user/bin/backup.sh >> /var/log/backup.log 2>&1'
You will see:
0 2 * * * /home/ec2-user/bin/backup.sh >> /var/log/backup.log 2>&1
What it means: 2>&1 merges stderr into the log.
Example 8: MAILTO off
Type this:
printf '%s\n' 'MAILTO=' '0 3 * * 0 /home/ec2-user/bin/weekly.sh'
You will see:
MAILTO=
0 3 * * 0 /home/ec2-user/bin/weekly.sh
What it means: Empty MAILTO stops email floods.
Example 9: Absolute interpreter
Type this:
printf '%s\n' '0 2 * * * /usr/bin/bash /home/ec2-user/bin/backup.sh'
You will see:
0 2 * * * /usr/bin/bash /home/ec2-user/bin/backup.sh
What it means: Avoid relying on cron PATH.
Concept 4: Common failures
Why: PATH and permissions cause most tickets.
What: absolute paths; matching user; verify syslog.
How: Test by hand before cron.
Example 10: Common failure PATH
Type this:
printf '%s\n' 'ERROR: aws: command not found' 'FIX: /usr/local/bin/aws or source profile'
You will see:
ERROR: aws: command not found
FIX: /usr/local/bin/aws or source profile
What it means: Cron does not load your interactive shell profile by default.
Example 11: Permission failure
Type this:
printf '%s\n' 'Permission denied' 'FIX: script owned by same user; mode 700'
You will see:
Permission denied
FIX: script owned by same user; mode 700
What it means: Root cron vs ec2-user paths differ.
Example 12: Verify last run
Type this:
printf '%s\n' 'grep backup /var/log/syslog | tail -3' '# or journalctl on some hosts'
You will see:
grep backup /var/log/syslog | tail -3
# or journalctl on some hosts
What it means: Confirm the job actually fired.
Install pattern
- Write /home/ec2-user/bin/backup.sh
- chmod 700
- crontab -e and add the line with redirects
- Wait for the minute or trigger with a short schedule in lab
- Read the log
Suppose: Retail app in Pune
Nightly tar of /var/www to /backup. Rotate after 7 days.
Suppose: US SaaS worker
Clean /var/log/myapp weekly. PATH bug fixed with /usr/bin/find.
Ravindra Bagale's Tip
Netflix-style and bank teams in Mumbai still trust cron for host-local backups. Learn fields, redirects and PATH failures before fancy schedulers.
Netflix-style आणि मुंबई bank teams अजून host-local backups साठी cron वर विश्वास ठेवतात. Fields, redirects, PATH failures शिका.
Netflix-style और मुंबई bank teams अभी भी host-local backups के लिए cron पर भरोसा करती हैं। Fields, redirects, PATH failures सीखें।
Check yourself (quiz)
Q1. What does 0 2 * * * mean?
02:00 every day.
Q2. Why >> log 2>&1?
Capture stdout and stderr.
Q3. Why absolute paths?
Cron PATH is small.
Q4. Safe mode for backup.sh?
700.
Q5. Dry-run deletes how?
find ... -print before -delete.
Reading tracking
Mark this guide when you finish. Your browser remembers it on this device.
Fields → script → crontab → logs → verify. Cron is still core DevOps.
Did you understand? Let us try it once more.
Frequently asked questions
What does 0 2 * * * mean?
Run at 02:00 every day.
Why >> log 2>&1?
Capture stdout and stderr.
Why absolute paths in cron?
Cron PATH is small.
Safe mode for backup.sh?
700.
How to dry-run deletes?
find ... -print before -delete.
How to confirm cron fired?
Check the job log or syslog/journal.