CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Scheduling / batch

batch

Run a job when the system is idle enough

Intermediateschedulingloadone-off

batch queues a command like at, but instead of a time it waits until the system load average drops below a threshold (1.5 by default), then runs it at lower priority. It suits heavy one-off work like re-indexing or compressing archives on a busy server.

Syntaxbatch [-f FILE] [-m] (same as: at -q b now)

Watch it run

Compress last week's logs on a busy app server without competing with production traffic.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
echo 'gzip -9 /home/devops/archive/*.log' | batch

The 'at' time is just now: batch jobs are eligible immediately but run only when the load allows.

Anatomy of a command

$
commandecho 'nice -n 19 gzip -9 /var/log/app/*.log.1'

The job text, read from stdin like at. The glob expands when the job runs, not now.

How it works

batch is a mode of the at program: it queues the job in queue 'b' with a run time of now, and atd stores it in /var/spool/cron/atjobs with the same environment-capturing script as at. The difference is in how atd starts it. Before launching a batch job, atd reads the 1-minute load average (getloadavg, the first number in /proc/loadavg) and only runs the job if it's below the limit: 1.5 by default, configurable with atd -l. It also starts at most one batch job per interval (atd -b, default 60 seconds), so a queue of heavy jobs trickles out one by one instead of all at once. Jobs in higher-lettered queues run with a higher nice value, so they get less CPU when the box is busy. While a job runs, atq shows it in the '=' queue. On a host whose load never drops below the limit, for example one with many cores, batch jobs can wait forever.

Options you'll actually use

-f FILERead the job from FILE instead of stdin
-mMail when finished
-q LETTERUse another queue (default b)
atq / atq -q bList pending jobs (only the batch queue)
atrm JOBCancel a job
atd -l LOADDaemon option: load limit for batch jobs (default 1.5)
atd -b SECONDSDaemon option: minimum gap between batch job starts

Common mistakes

Expecting batch to run 'tonight'

batch has no time argument; it runs as soon as the load is low, which may be right now.

echo cmd | at 02:00 # or at -q b 02:00 for a timed batch-queue job
Using batch on a many-core server with the default settings

The 1.5 limit is absolute, not per core, so normal load on a big box keeps jobs waiting forever.

Run atd with -l <cores x 0.75>, or use nice/ionice from at or systemd-run
Assuming batch makes a job 'low priority' in every way

It only delays the start and adds a small nice value. Once running, a disk-heavy job still hurts I/O.

echo 'nice -n 19 ionice -c3 /opt/heavy.sh' | batch

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Queue 'sleep 30; date > /tmp/batch-done.txt' with batch and check which queue it's in.

  2. 2

    Check the current 1-minute load average and decide whether atd will start a batch job now.

  3. 3

    Queue a CPU-heavy job through batch that also runs at the lowest CPU priority.

Related commands

atRun a command once at a later timeniceStart a command with a lower (or higher) CPU prioritycrontabSchedule recurring jobs with cron
PreviousatNextsleep
Linux / Scheduling / batch

batch

Run a job when the system is idle enough

Intermediateschedulingloadone-off

batch queues a command like at, but instead of a time it waits until the system load average drops below a threshold (1.5 by default), then runs it at lower priority. It suits heavy one-off work like re-indexing or compressing archives on a busy server.

Syntaxbatch [-f FILE] [-m] (same as: at -q b now)

Watch it run

Compress last week's logs on a busy app server without competing with production traffic.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
echo 'gzip -9 /home/devops/archive/*.log' | batch

The 'at' time is just now: batch jobs are eligible immediately but run only when the load allows.

Anatomy of a command

$
commandecho 'nice -n 19 gzip -9 /var/log/app/*.log.1'

The job text, read from stdin like at. The glob expands when the job runs, not now.

How it works

batch is a mode of the at program: it queues the job in queue 'b' with a run time of now, and atd stores it in /var/spool/cron/atjobs with the same environment-capturing script as at. The difference is in how atd starts it. Before launching a batch job, atd reads the 1-minute load average (getloadavg, the first number in /proc/loadavg) and only runs the job if it's below the limit: 1.5 by default, configurable with atd -l. It also starts at most one batch job per interval (atd -b, default 60 seconds), so a queue of heavy jobs trickles out one by one instead of all at once. Jobs in higher-lettered queues run with a higher nice value, so they get less CPU when the box is busy. While a job runs, atq shows it in the '=' queue. On a host whose load never drops below the limit, for example one with many cores, batch jobs can wait forever.

Options you'll actually use

-f FILERead the job from FILE instead of stdin
-mMail when finished
-q LETTERUse another queue (default b)
atq / atq -q bList pending jobs (only the batch queue)
atrm JOBCancel a job
atd -l LOADDaemon option: load limit for batch jobs (default 1.5)
atd -b SECONDSDaemon option: minimum gap between batch job starts

Common mistakes

Expecting batch to run 'tonight'

batch has no time argument; it runs as soon as the load is low, which may be right now.

echo cmd | at 02:00 # or at -q b 02:00 for a timed batch-queue job
Using batch on a many-core server with the default settings

The 1.5 limit is absolute, not per core, so normal load on a big box keeps jobs waiting forever.

Run atd with -l <cores x 0.75>, or use nice/ionice from at or systemd-run
Assuming batch makes a job 'low priority' in every way

It only delays the start and adds a small nice value. Once running, a disk-heavy job still hurts I/O.

echo 'nice -n 19 ionice -c3 /opt/heavy.sh' | batch

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Queue 'sleep 30; date > /tmp/batch-done.txt' with batch and check which queue it's in.

  2. 2

    Check the current 1-minute load average and decide whether atd will start a batch job now.

  3. 3

    Queue a CPU-heavy job through batch that also runs at the lowest CPU priority.

Related commands

atRun a command once at a later timeniceStart a command with a lower (or higher) CPU prioritycrontabSchedule recurring jobs with cron
PreviousatNextsleep