Chapter 60: Bash Schedule (cron)

Bash Schedule (cron)

This is one of the most powerful and practical things you can learn in Bash/Linux — once you understand cron, your computer becomes your personal robot assistant that works even when you are sleeping, eating, or out with friends in Hyderabad.

cron is the built-in Linux scheduler — it lets you tell the system:

“Hey computer, please run this command / this script / this backup / this cleanup / this reminder every day at 2 AM, or every Monday at 9 AM, or every 5 minutes, or once a month — and do it automatically forever.”

No need to keep your laptop on or remember anything — cron does it silently in the background.

What Does “cron” Stand For?

cron = from Greek Chronos (time) It comes from the old Unix days (1970s) — and it’s still the king of scheduling in 2026 on almost every Linux server, Raspberry Pi, cloud instance (AWS, GCP, DigitalOcean), and even your Ubuntu laptop.

Two Main Parts of cron

  1. crontab — your personal schedule file (you edit this) → There is one crontab per user (yours is separate from root’s)
  2. cron daemon (crond) — the background service that reads all crontabs every minute and runs what’s due

The Crontab Format – The Most Important Thing to Memorize

Every line in crontab has exactly 6 fields (space-separated) + the command:

text

Each field can be:

  • A number
  • * = every possible value
  • Range (1-5)
  • List (1,3,5)
  • Step (*/15 = every 15)
Field Allowed values Meaning / Example
1. Minute 0–59 0 = on the hour, 30 = half past
2. Hour 0–23 0 = midnight, 14 = 2 PM
3. Day of month 1–31 1 = first day, 15 = middle of month
4. Month 1–12 1 = January, 12 = December
5. Day of week 0–7 (0 & 7 = Sunday) 0/7=Sun, 1=Mon, 5=Fri, 6=Sat
6. Command Full path or whatever /home/webliance/backup.sh or echo “Hi”

Real Examples – Let’s Memorize These Patterns

When you want it to run Crontab line example Explanation
Every day at 2:00 AM 0 2 * * * /home/webliance/backup.sh 0 minute, 2 hour, every day/month/weekday
Every 15 minutes */15 * * * * /usr/bin/echo “Hello” >> ~/log.txt Every 15 min (0,15,30,45)
Every Monday at 9:30 AM 30 9 * * 1 ~/scripts/weekly_report.sh 30 min, 9 hour, any day/month, Monday (1)
Every 1st of month at midnight 0 0 1 * * ~/scripts/monthly_backup.sh 0 min, 0 hour, 1st day, every month
Every weekday (Mon–Fri) at 8 AM 0 8 * * 1-5 ~/scripts/start_work.sh 0 min, 8 hour, Mon to Fri
Every 2 hours (0,2,4,6,8,10,12,14,16,18,20,22) 0 */2 * * * ~/scripts/check_server.sh Every 2 hours on the hour
Every 30 seconds (cron minimum is 1 minute) Not possible directly — use sleep in loop or systemd timer

How to Edit Your Crontab (The Safe Way)

Never edit /etc/crontab directly — use your user crontab:

Bash

→ Opens your personal crontab in default editor (nano/vim)

First time it asks which editor → choose nano if you’re new.

Add lines like:

Bash

Save & exit → cron installs it automatically.

View Your Crontab

Bash

Remove All Your Jobs (Be Careful!)

Bash

Very Common Real-Life Examples (Copy-Paste Ready)

  1. Daily backup at 3 AM
Bash
  1. Clean old logs every Sunday at 4 AM
Bash
  1. Run a script every 10 minutes
Bash
  1. Reminder every weekday at 9 AM
Bash

Important Teacher Warnings & Best Practices

  • Always use full paths — cron has limited $PATH → Write /usr/bin/tar not just tar

  • Redirect output & errors — otherwise cron emails root (annoying)

    Bash
  • Test first — run the command manually before putting in crontab

  • Use absolute paths for scripts

    Bash
  • Minute field minimum = 1 minute — no seconds

  • Don’t overload — too many jobs at same time can slow system

  • Check logs — if nothing happens, look in /var/log/syslog or /var/log/cron

    Bash

Quick Summary Table – Crontab Patterns

You want… Crontab line example
Every day at midnight 0 0 * * *
Every hour on the hour 0 * * * *
Every 30 min 0,30 * * * * or */30 * * * *
Every weekday 9 AM 0 9 * * 1-5
1st & 15th of every month 0 0 1,15 * *
Only Fridays at 5 PM 0 17 * * 5

Got it, boss? cron turns your Bash scripts into automatic background workers — backups, cleanups, reminders, health checks, reports — all running silently while you sleep.

Any part confusing? Want next:

  • “Teacher, explain systemd timers (modern replacement for cron)”
  • “How to debug cron jobs that don’t run”
  • “Cron special strings (@daily, @weekly, @reboot)”
  • “Run script every X seconds (workarounds)”
  • “User vs system crontab (/etc/cron.d vs user crontab)”

Just say — teacher is ready in Hyderabad! Start with one small cron job today — even something simple like “echo hi >> ~/cron_test.log every 5 minutes” — you’ll feel the magic! 🐧⏰🔄 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *