Unix - Scheduling in Unix

Scheduling in Unix

Unix provides two main tools for scheduling jobs:

  • cron → for repeated/periodic tasks (like every day, every hour, every Monday at 2 AM, etc.)

  • at → for one-time tasks (run once at a specific date/time)


1. cron – Periodic Jobs

How cron works

  • Runs the cron daemon (crond) in the background.

  • Jobs are defined in a file called the crontab (cron table).

Crontab Format

A cron job entry has 6 fields:

MIN  HOUR  DAY  MONTH  WEEKDAY  COMMAND
  • MIN → 0–59

  • HOUR → 0–23

  • DAY → 1–31

  • MONTH → 1–12

  • WEEKDAY → 0–6 (0 = Sunday)

  • COMMAND → the script/command to execute

Examples

crontab -e         # Edit your crontab file
crontab -l         # List your cron jobs

Jobs:

0 2 * * * /home/user/backup.sh        # Run backup script every day at 2:00 AM
30 9 * * 1 /home/user/report.sh       # Run every Monday at 9:30 AM
*/5 * * * * echo "Hello" >> /tmp/hi   # Run every 5 minutes

2. at – One-Time Jobs

How at works

  • You schedule a job to run once at a specific time.

  • The atd daemon must be running.

Syntax

at TIME

Then type the commands, and press Ctrl+D to finish.

Examples

echo "echo Hello > /tmp/test.txt" | at 5pm     # Run at 5 PM today
at 11:00pm                                     # Start interactive mode
> echo "Backup started" >> /tmp/log
> Ctrl+D                                       # End input

View scheduled jobs:

atq          # List pending at jobs
atrm <jobid> # Remove a scheduled job

3. Comparison

Feature cron at
Repeats Yes (periodic) No (one-time only)
Syntax Defined in crontab Run with at <time>
Use Case Backups, reports, cleanup scripts Running something at a specific time (like tomorrow 3 PM)

Summary:

  • Use cron when you need repeated scheduling (daily, weekly, monthly).

  • Use at when you need a one-time scheduled task.