Ravindra BagaleCourses & study guides मराठी Track your progress

Guides

Cron Jobs समजावून: Backups आणि Log Cleanup शेड्यूल करा

Linux वर cron ने backups आणि log cleanup. EC2/on-prem सारखे fields आणि PATH fixes.

Friends, cron is boring until backups miss a night. Chala mitrano — log every run.

Quick answer

Hold this vertical picture:

  1. पाच fields.
  2. Script काम करते.
  3. Logs redirect.
  4. PATH/permissions.
  5. Verify.

Language: English · मराठी · हिंदी

Write scriptbackup or cleanup chmod 700owner only crontab linefields + absolute path Redirect logs>> file 2>&1 Verify runsyslog or log file
Cron job path

Cron अजून का महत्त्वाचे

Cloud schedulers असले तरी Amazon EC2 आणि on-prem ला host-local cleanup हवे. Backup चुकल्यास INR आणि dollars दोन्ही जातात.

Why: Unattended work विश्वासार्ह हवे. What: crontab fields, backup/cleanup, failure fixes. How: Script, schedule, log सिद्ध करा.

Concept 1: Crontab fields

Why: चुकीचा तास = चुकीची रात्र. What: min hour dom mon dow. How: एका 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:

no-crontab-yet

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/cleanup scripts

Why: Cron फक्त launch करतो. What: tar; find old logs. How: -print ने dry-run.

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

Why: Silent job failure लपवते. What: >> log 2>&1; MAILTO=. How: 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: Failures

Why: PATH आणि permissions. What: absolute paths; user match; syslog. How: Cron आधी हाताने test.

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

  1. backup.sh लिहा
  2. chmod 700
  3. crontab -e
  4. log वाचा

Pune retail

Nightly tar /var/www.

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.

Check yourself (quiz)

Q1. 0 2 * * *?

दररोज 02:00.

Q2. >> log 2>&1?

stdout+stderr.

Q3. Absolute paths का?

PATH लहान.

Q4. backup.sh mode?

700.

Q5. Dry-run?

-print आधी -delete.

Reading tracking

Mark this guide when you finish. Your browser remembers it on this device.

Fields → script → crontab → logs → verify. 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.