Job Scheduling commands (at, batch, crontab)

Some jobs should run without you sitting there: at runs a command once at a future time, batch runs one when the system is free, and crontab schedules commands to repeat on a timetable set by five fields, minute, hour, day, month, weekday.

11 min read · 8 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Work that runs while you sleep

You wrote a script that backs up your project folder. You do not want to remember to run it every night at 2am; you want the system to run it for you, automatically, on schedule.

That is job scheduling. Linux gives you three tools: at for a one-off future run, batch for running when the machine is idle, and crontab for jobs that repeat on a timetable. The recurring one, cron, is the workhorse, and its five-field time format is the main thing to learn here.

At a glance

CommandWhen it runs the job
at TIMEOnce, at a specified future time (e.g. at 2am)
batchOnce, whenever the system load is low enough
crontab -eRepeatedly, on a schedule you edit (the timetable)
crontab -l(Lists your current scheduled cron jobs)

Theory

The five cron fields

A cron schedule is five time fields, read left to right, followed by the command:

minute hour day-of-month month day-of-week command

  • minute: 0 to 59
  • hour: 0 to 23 (24-hour clock)
  • day-of-month: 1 to 31
  • month: 1 to 12
  • day-of-week: 0 to 6 (0 or 7 is Sunday, 1 is Monday)

An *asterisk ``** in a field means 'every' value of that field. So you build a schedule by saying, in each field, exactly when to fire.

Practical

Scheduling the backup with cron

# Edit your crontab:
$ crontab -e

# Add this line: run backup.sh at 06:30 every Monday
30 6 * * 1 /home/riya/backup.sh
#  |  | | | |
#  |  | | | +-- day-of-week: 1 = Monday
#  |  | | +---- month: * = every month
#  |  | +------ day-of-month: * = every day-of-month
#  |  +-------- hour: 6
#  +----------- minute: 30    ->  06:30

# Run every day at midnight instead:
0 0 * * * /home/riya/backup.sh

Formula

Read the five fields left to right

The whole skill of cron is reading those five fields in order: minute, hour, day-of-month, month, day-of-week. Star means every.

So 30 6 * * 1 is minute 30, hour 6, any day-of-month, any month, weekday 1 (Monday), that is 06:30 every Monday. And 0 0 * * * is minute 0, hour 0, every day, that is midnight every day. Get comfortable translating a line into plain English and back; that is what exam questions and real setups both ask.

Quiz

A crontab contains the line: 0 22 * * 5 /home/riya/report.sh . When does report.sh run?

  1. At 22 minutes past every hour, every day
  2. At 22:00 (10 pm) every Friday
  3. On the 22nd of every month
  4. Every 5 minutes
Show the answer

At 22:00 (10 pm) every Friday

Read the five fields left to right: minute 0, hour 22, day-of-month (any), month (any), day-of-week 5. Day-of-week 5 is Friday (0/7=Sunday, 1=Monday, so 5=Friday). So it runs at hour 22 minute 0, which is 22:00 or 10 pm, every Friday. Option A misreads the fields: the first field 0 is the minute and 22 is the hour, giving 22:00, not '22 minutes past every hour'. Option C confuses day-of-week (field 5) with day-of-month (field 3); the 22 here is the hour, and the 5 is the weekday. Option D invents an 'every 5 minutes' meaning that the fields do not express. Always parse minute, hour, day, month, weekday in that order.

Think first

When do you use at instead of cron?

Both at and cron schedule jobs. How do you decide which one to reach for? Then tap.

Show the answer

Choose by whether the job repeats. Use at for a ONE-OFF future job, and cron for a RECURRING one. If you need something to happen exactly once at a specific later time, 'run this cleanup at 2am tonight', 'send this reminder next Monday at 9', then at is perfect: you give it a single time, it runs the job once, and it is done. If instead you need something to happen again and again on a timetable, 'back up every night', 'generate a report every Friday at 10pm', then cron (crontab) is the tool, because it fires on the recurring schedule described by the five fields, indefinitely, until you remove it. batch is a special case of the one-off idea: run once, but not at a fixed time, rather whenever the system is not busy, useful for heavy work you do not want competing with users. So the decision tree is simple: repeats on a schedule -> cron; happens once at a set time -> at; happens once when the machine is free -> batch. Match the tool to the rhythm of the job.

Summary

Key takeaways

  • Job scheduling runs commands automatically, without you being present.
  • at runs a command once at a future time; batch runs it once when system load is low.
  • crontab schedules recurring jobs (crontab -e to edit, crontab -l to list).
  • A cron line has five time fields then the command: minute, hour, day-of-month, month, day-of-week.
  • An asterisk * means 'every'; day-of-week 0 or 7 is Sunday, 1 is Monday, 5 is Friday.
  • Example: 30 6 1 runs at 06:30 every Monday; 0 0 * runs at midnight every day.
  • Memory hook: recurring -> cron (five fields), once at a time -> at, once when idle -> batch.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Basic Linux Commands

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Job Scheduling commands (at, batch, crontab) · Linux Operating System (LOS) (Minor-04) · Gri-Learn