-->
Previous Table of Contents Next


The at Command

The at command expects a time or date as a parameter and takes any number of command strings from its standard input. When the at command detects an end-of-file marker, it creates a Bourne shell script for execution at the time you specified.

The at command is flexible about the types of dates and times it accepts. For example, if you enter the command at now + 1 day, the next commands, taken from the standard input, are executed tomorrow at this time. One way to use the at command is from within a shell script.

A shell script is nothing more than a file containing all the commands necessary to perform a series of commands. The name of the file then becomes your own addition to the Linux command language. One way of using the at command is shown here:


at now + 1 day

command-1

command-2

When placed in a shell script, these lines let you conveniently run one or more commands the next day. To run any number of different commands, simply enter new commands after the at command line. You can run any number of commands from this script.

The batch Command

The batch command is the logical equivalent of at now. If you attempt to use the at now command, you see an error message that says something along the lines of now has passed. The batch command works exactly as at now works if it were logically possible, with one minor exception: The cron daemon maintains a separate queue for commands generated by at, batch, and cron. Suppose that you entered the following commands into the file named backup:


tar  -cvf  tackettbkup  /usr/home/tackett

Then you can tell the system to back up the directory /usr/home/tackett by using this command


batch backup


See “Creating Your First vi File,” p. 182

The crontab Command

One of the best uses of the cron daemon is in automating the maintenance of a system. With cron, you as the system administrator can set up automatic backups of your system every morning at 4 a.m., Monday through Saturday. You install, delete, and list commands you want run in this fashion with the crontab command.

To run commands periodically, you must create a file in the crontab format. The crontab file consists of six fields separated by spaces or tabs. The first five fields are integers specifying minute (00–59), hour (00–23), day of the month (01–31), month of the year (01–12), and day of the week (0–6, with 0 referring to Sunday). The sixth field is a command string. Each numeric field can contain an inclusive range of numbers (such as 1–5 to indicate Monday through Friday) or discrete sets of numbers (such as 0,20,40 to indicate that an instruction should be run every 20 minutes). A field can also contain an asterisk to indicate all legal values.

The following example runs the calendar command every 20 minutes, starting at midnight Monday and ending at 11:40 p.m. Friday:


0,20,40 * * * 1-5 calendar -

If you name this file cronfile, you can install it in the cron system by issuing the command crontab cronfile.

The cron daemon has a time granularity of one minute—meaning, the shortest time duration you can work with is one minute. You, as system administrator, can place limits on the number of commands allowed to be run at any one time. Just because you ask cron to run an at, batch, or crontab file doesn’t mean that it runs at precisely the time you’ve indicated.

Understanding Command Feedback

Linux provides instant feedback for commands that abort for one reason or another. In most cases, errors are limited to misspellings of the command name or badly formed filenames. If you attempt to run a nonexistent command, Linux replies with


command: command not found

If you try to use a nonexistent filename, Linux responds with


command: file: No such file or directory

If the error is caused by something other than a command-line error, the command itself usually reports what happened—although not always in an easily decipherable form.

If you try to run a command with nohup and haven’t redirected the standard error, Linux automatically places any error messages in a file named nohup.out in the directory from which the command was run.

Because commands run by cron have less urgency, any errors—indeed, any output placed on the standard output and not redirected—is sent to you through e-mail.

Editing and Aliasing Shell Commands

Different shells include features that provide shortcuts for running commands. Command editing lets you modify commands that have already been typed in. By using Linux’s command history feature, you can recall commands you’ve previously entered. Aliasing lets you create commands that represent other commands. Command completion lets you fill in the rest of a filename after you type part of it.

Editing Commands

Command editing means that after you type a command—and before you press <Return>—you can edit or change parts of the command without having to retype most of it. To edit a command, press <Esc> to get into editing mode and then use any of the line-movement commands from the vi editor to modify the command. You can use <Backspace> to return to the portion of the command you want to change, and use other vi commands, such as x to delete a character, r to replace a character, and so on.

Viewing Command History

The command history feature allows you to look back at previously entered commands and recall them. This feature saves you the time and trouble of retyping commands. When you combine this feature with command editing, you can easily correct mistakes in complicated commands and deal effectively with some repetitive tasks.

In both shells, the history command displays the list of past commands the shell has saved. The commands are numbered. To execute command 10, for example, enter ! 10. The bash shell also takes advantage of your PC’s arrow keys; you can recall previous commands by pressing the <↑> key.


Previous Table of Contents Next