NEXT UP previous
Next: Archives

Batch Jobs

Usually, when you enter a command, it is with the intention that it should execute immediately. Sometimes, however, a user will want to have a command executed some later time as a batch job, perhaps even after the user has logged off.

cron

There is a program called crond which most systems arrange to run some time during the boot up sequence. The 'd' on the end of the name indicates that this is the cron daemon. In common with many daemons, this program continues to run all the time the machine is running and its sole function is periodically to examine the contents of a set of command files and execute the commands in these files at appropriate times.

The command files each consist of a set of command lines. The general format of a command line is:

	minutes hours days months day_names command

The minutes, hours, days, months and day_names fields allow you to specify when the associated command will be executed. For example:

	25 8 27 Jan * birthday

At 08:25 am every 27 January crond will execute a user's program called birthday. The star (*) in the day_names field means 'on whichever day it occurs'.

The star generally means 'match any possible value' and it can be used in the other date and time fields as well:

	25 8 * * * appointments

This says at 08:25 am every day/month/day_name run the user program called appointments.

If you only wanted to run appointments on working days (i.e. Monday to Iriday) then these can be listed in the day_nanes field, separated by commas:

	25 8 * * mon,tue,wed,thu,fri appointments

Lists of consecutive values, like those in the day_names field, can also be specified as a simple range, and have the same meaning:

	25 8 * * mon-fri appointments

If you specify days and day_names in the same command line, then both of them apply:

	0,15,30,45 * 1 * mon oddtimes        

This example will execute the user command oddtimes every 15 minutes on the first day of every month and on every Monday.

Every user login can have its own associated command file to use with crond. These command files are stored in the directory:

	/usr/spool/cron/crontabs

However, ordinary users do not have direct access to this directory so they must use a special command to create and modify their crond command files. This command is called crontab. There are several command line switches which can be used to get the crontab command to create, modify and delete your personal command files. The main options and command line switches to crontab are:

file [-u user]replace current command file with file;
- [-u user]replace current command file from standard input;
-1 [user]list current command file;
-e [user]edit current command file with vi;
-d [user]delete current command file;
-c dirspecify a new command file directory.

Only root can specify the -c switch to change the directory used to hold the command files, and only root can give a user name on the end of the command line to apply the given option to the specified user's command file. Don't forget that the square brackets are being used here to enclose optional parameters and should not be entered.

at

The use of crontab entries will allow given commands to be executed on a regular basis. However, it may happen that you only have a batch job which you would like to submit to the system to be executed just once, at some specified time. In order to accommodate this requirement there is another command, called at which still relies on crond for its operation but which will give the required 'one shot' functionality. In order for at to work there is a special command line in the crontab file for root. It appears as:

	0,5,10,15,20,25,30,35,40,45,50,55 * * * *   /usr/lib/atrun         

This line causes /usr/lib/atrun to be executed every five minutes. What atrun does when it executes is to look through the pending list of at command jobs, to find any that need to be run at the current time, and to run them. Submitting an at job is easy. The general form of the at command is:

	at [-f file] time 

This will execute all the commands given in file at the specified time. If the optional file switch is not given, then the commands to execute will be read from the standard input instead (keyboard by default):

	$ at 10:55      
	date >/tmp/at.job.test 
	Ctrl-d  
	Job c00cd3093.00 will be executed using /bin/sh         

Remember that pressing Ctrl-d on a line on its own is the way to generate an end-of-file from the keyboard. Notice that the at command responds at the end of the sequence by allocating a unique job reference number (c00cd3093.00 in this case). The user can check up on the queue of outstanding jobs by using the atq command:

	$ atq
	Date			Owner	Queue	Job#
	10:55:00  07/27/95   	pc      c       c00cd3093

Checking on the file /tmp/at.job.test after 10:55 am gives:

	$ cat /tmp/at.job.test
	Thu Jul 27 10:55:02 BST 1995         

It is also possible to remove an at job from the queue by using the atm command followed by the job reference number.

The at command allows you to specify the job run times in some fairly sophisticated formats including things like:

	10am Aug 20
	6pm tomorrow            
	7:21 + 4 days

The last example means 7:21am in four days' time. In addition, dates can also be given in several formats, including:

	MM/DD/YYDD.MM.YY        

which allows the day and month to be specified either way round.

batch

There is another command, intimately related to at, which performs a similar function, called batch. Entering a batch command is just the same as at except that no time needs to be specified. The submitted job will then be run at a time which depends on the load average of the machine, when the machine is sufficiently lightly loaded, the job gets done. The use of the batch command depends upon you having installed the /proc filesystem from your Linux distribution, because it needs to read the machine's load average from the file:

	/proc/loadavg

Access to the at and batch facilities can be allowed or denied on an individual user basis by root. The rules are as follows:

The default for the Slackware distribution is that /etc/at.deny exists and is empty, thus allowing everyone access to the facilities.


NEXT UP previous
Next: Archives