Next | Prev | Up | Top | Contents | Index

Automating Tasks with at(1), batch(1), and cron(1M)

You can use the at(1), batch(1), and cron(1M) utilities to automate many of your usual tasks, both as an administrator and as a user. These utilities perform similar functions. All execute commands at a later point in time. The difference between the commands is that at executes the given command at one specific time; cron sets up a schedule and executes the command or commands as often as directed, according to the schedule; and batch executes the commands when system load levels permit the execution.


at(1) Command

If you have a task to process once at a later point in time, use at. For example, if you wish to close down permissions on a public directory at midnight of the current day, but you do not need to be present when this occurs, you could use the command string:

at 2400 July 14

chmod 000 /usr/public

<Ctrl-D>

It is required that the at command itself and the date and time of the command be placed alone on a line. When you press <Return>, you do not see a prompt; at is waiting for input. Enter the command to be executed just as you would type it at a shell prompt. After entering the command, press <Return> again and enter <Ctrl-D> to tell at that no more commands are forthcoming. You can use a single at command to execute several commands at the appointed time. For example, if you want to close the public directory and change the message of the day to reflect this closure, you can create the new message of the day in the file /tmp/newmesg, and then issue the following command string:

at 2400 July 14

chmod 000 /usr/public

mv /etc/motd /etc/oldmotd

mv /tmp/newmesg /etc/motd

<Ctrl-D>

By default, any output of commands executed using at is mailed to the executing user through the system electronic mail. You can specify a different location for the disposition of output by using the standard output redirects, such as pipes (|) and file redirects (>). See your command shell documentation for a complete description of redirecting the standard output.

For complete information on the at command, see the at(1) reference page.


batch(1) Command

The batch command works just like the at command, except that you do not specify a time for the command or commands to be executed. The system determines when the overall load is low enough to execute the commands, and then does so. As with all other cron subsystem commands, the output of the commands is mailed to you unless you specify otherwise. batch is useful for large CPU-intensive jobs that slow down the system or cripple it during peak periods. If the job can wait until a non-peak time, you can place it on the batch queue until the system executes it. For complete information on the batch command, see the batch(1) reference page.


cron(1M) Command

If you desire to have a command executed regularly on schedule, the cron command and subsystem provide a precise mechanism for scheduled jobs. The at and batch commands are technically part of the cron subsystem and use cron to accomplish their tasks. The cron command itself, though, is the most configurable command of the subsystem.

You use cron by setting up a crontab file, where you list the commands you would like to have executed and the schedule for their execution. Complete information on setting up your crontab file is available in the cron(1M) and crontab(1) reference pages.

The cron facility is useful for scheduling network backups, checking the integrity of the password file, and any other scheduled tasks that do not require interaction between you and the system. By default, cron mails the results or output of the command to the user who submitted the crontabs file, so if you use cron to schedule something like a pwck(1M), the results of the test are mailed to you and you can interpret them at your convenience.

Note that you must restart cron after each change to a crontabs file, whether made through the cron utility or the at command, for the changes to take effect.


Next | Prev | Up | Top | Contents | Index