-->
Previous Table of Contents Next


Complex cron Commands

The crontab file can contain any type of command or shell script, as long as the line is valid (in other words, it can be executed from the shell prompt). A common problem with many shell commands is the generation of output, especially error messages, which are mailed to you and can clog up your mailbox quickly. For this reason, if you anticipate error message output (from a compiler, for example), you can redirect the output to /dev/null. For example, the following command sends the output of the date command to a file called /tmp/test1 every hour on the hour and sends any error messages to /dev/null (which essentially discards such messages):


0 * * * * date > /tmp/test1 2>/dev/null

You can do the same with the standard output, if you want, or you can redirect it elsewhere. For example, the cron command concatenates all the files starting with “chapt” in /usr/tparker into one large file called /usr/tparker/archive/backup:


30 1 * * * cat /usr/tparker/chapt* > /usr/tparker/archive/backup

Again, the standard error could be redirected.

You can also do piping in the crontab file. For example, if you have a list of users who are logged in to the system during the day in the file /tmp/userlist, you could have a crontab entry that looks like this:


0 1 * * * sort -u /tmp/userlist | mail -s”users for today” root

This line sorts the output of /tmp/userlist so there is only one entry for each user (the -u or unique option) and mails it to root.

An important point to remember with cron is that all commands are executed, by default, in the Bourne shell (or bash, if it is the sh equivalent on your system). If you use C shell commands, the cron task will fail.

The at Program

The at program is very similar to cron except it executes a command only once at a prespecified time (whereas cron keeps executing it). The format of the at command is


at time date < file

Most of the parameters to be used with the at command can be specified several ways, essentially to make at more versatile. The time, for example, can be specified as an absolute time (18:40 or 22:00), as two digits which are taken as hours (so 10 means ten o’clock in the morning because a 24 hour clock is the default). You can add an “a.m.” or “p.m.” to the time to make it clear which you mean, so 10 p.m. is unambiguously in the evening.

The at command handles a few special words instead of time designations. The command recognizes the words “noon,” “midnight,” “now,” “next,” and “zulu” for GMT conversion. (Some at versions generate an error message if you try to execute a command with the time set to “now.”)

The date is an optional field and should be used when the time is not specific enough. In other words, when a date is not supplied, the next instance that the specified time occurs, the command executes whereas with a specified date, the date must match as well. The date can be given as a month’s name followed by a day number (May 10) or a day of the week (either spelled out in full or abbreviated to three characters). You can specify a year, if you want, but this is seldom necessary.

As with the time, the at command recognizes two special words: “today” and “tomorrow” (although the word “today” is redundant as the command will execute today if the time is set properly).

The file to be read as input to the at command can be any file with commands in them. Alternatively, you can enter the commands at the keyboard, terminating with a Ctrl+D, although this is not recommended because of the high potential for error.

Suppose you have a file called reorg.data with the following commands in it and the file is made executable:


/usr/tparker/setperms

/usr/tparker/sort_database

/usr/tparker/index_database

/usr/tparker/clean_up

If you want to execute this file at 8:30 p.m., you can issue any one of the following commands using at:


at 20:30 < reorg.data

at 8:30 pm < reorg/data

at 20:30 today < reorg.data


Previous Table of Contents Next