CLI magic: not your father’s batch

26

Author: Joe Barr

I’ve heard old-timers say that back in the day, when pulling an all-nighter at the data center, they would put hot dogs on top of an IBM S/360 mainframe to warm them up. Now that’s what I call a classical batch environment. These days, batch is a whole ‘nother thing. So this week our excuse to break free of those GUI chains for awhile will be to take a look at a handy little program called batch, natch.

Let’s say that you want to compile X.org‘s alternative to the XFree86 window manager, but that you don’t want the compile to bog down your system while it’s cooking, because you’re busy burning Knoppix CDs to give to your unenlightened friends, or something else equally as important.

No problem. Just enter the commands to decompress and compile the X.org window manager source code and save them to a text file called “batchmake”. That file might look something like this:


tar xzf X11R6.7.0-src1.tar.gz
tar xzf X11R6.7.0-src2.tar.gz
tar xzf X11R6.7.0-src3.tar.gz
cd xc
make World > World.log 2>&1

Once those commands are saved as a file, you can turn the job over to batch simply by entering:


batch -f batchmake

Batch is actually a simple shell script which executes a sophistical tool for the delayed execution of tasks called at. Here is what the batch script looks like on my system in /usr/bin:


#! /bin/sh
prefix=/usr
exec_prefix=${prefix}
exec ${exec_prefix}/bin/at -qb now "$@"


The first parameter being passed to the at program on the final line of the batch script above is “-qb.” The -q is used to assign the task to a specific queue. In this case, the b following the -q indicates that the job will run in the batch queue. Running as batch in this environment doesn’t mean stacks of punched card or mag tape input and boxes of greenbar output. It means “Don’t run this job until system activity is at or below a specific level.”

And just what is that specific level, you ask? Good question. Requesting at run as a job as batch means “when the load average drops below 0.8.” You can think of system load as being the number of tasks running at a given time. Load averaging is a little more complex. There is an excellent discussion of the topic here.

For our purposes, it isn’t really necessary to have a firm mathematical grasp of the how its derived. Just know that it’s a sometimes useful metric indicating how busy your box is. And that means we can say that running a job as batch in our case simply means “Run it when you’re not too busy.”

When should I start?

Now. The second argument that the batch command passes to the at program is “now”. While the simplicity and clarity of that “timespec” is remarkable, it would be a major mistake that at is only capable of the most basic scheduling commands. In fact, the at program includes such sophisticated scheduling abilities that the man pages don’t really address them.

The at man page tells us more about the at timespec. For example, you can specify at run a job at teatime, or noon, or midnight. You can also specify an exact time, or date, for the job to run. And you can combine them by saying something like “4pm + 3 days”. The man page points readers to an entire document (on my system is is at /usr/share/doc/packages/timespec) on the at timespec.

What the $@?

The final argument in the command string (the “$@” on the last line of the batch script) denotes all the variables passed to the script from the command line. Remember when we entered our batch command above, we added the “-f batchmake” argument. The man page for the at program explains that the “-f” option tells the program to use the file named as standard input rather than the keyboard. Thus, at attempts to execute each line in the file you’ve named until they are exhausted.

Batch represents yet another example of a command-line tool which “dumbs down” another program by eliminating complexity for the user. You may or may not have occasion to use it, but it’s real handy when you do.