The at and cron Commands


The at and cron Commands

Thus far we have only touched on commands that will allow you to automate processes, because automation is the easiest way to get things done. If you know how to automate something, then it's as simple as setting up the job one time and having it run on a schedule. In this section of the lesson, we will talk about how the at and cron commands can help you automate tasks in Unix. There is a great deal to talk about when considering backups and running complex processes with these commands, and we cannot dig into these topics in-depth in a book of this nature. For now, be aware that these commands are powerful and there is much more to learn about them. You may want to dig into this subject a bit deeper if you continue to work with Unix.

So, when considering backups in particular but any process overall, Unix users may need to schedule jobs with the at and cron commands. Let's look at how this can be done.

Hey YouBack Up! Backups are a part of life when you want to make sure that your data (files and directories) are recoverable in case you delete something or make a mistake.

Unix is not forgiving, so if you do make a mistake, your only recourse is to get a copy of the missing or deleted data. You would get this copy from a backup source, most likely a tape backup system if you are at work. If you are at home, just about any media device can be used for backup. Remember, backups are important, because if you ever need a copy of your data, backups ensure that the copy is there.


There are two basic ways to set up your Unix system to run a command (process) at a specific time; one way is to use the cron command, and the other is to use the at command. Your Unix system will most likely always have the cron process running, but in many cases, if this command is not needed, it will be disabled by your Unix system administrator. (For security reasons, it is common in most environments to always terminate, disable, and remove anything from a system that its users don't need to do their work. If you are running Unix at home for fun, then you may have just about everything running on your system if you are logged in as root or a SuperUser.) In any case, cron (if enabled) will allow you to automate complex jobs such as log rotations, backups, cleanup commands, and so on. The cron command will do this on a continuous schedule that you configure. cron enables you to run commands in intervals as small as one second or as long as one year.

You should also know what a crontab is, because this term will most certainly come up when you work with a Unixor Linux-based operating system. You can create a personal crontab file that holds information about the interval on which you want a command to run. You can then use the crontab <filename> command to add your request to the system's crontab file. With some Unix versions, you can use crontab -e to bring up your currently set crontab enTRies directly into an editor as well.

Using cron

As mentioned earlier in the lesson, the cron process (also called the cron daemon) executes commands at specific dates and times. You can use cron to schedule activities, either as one-time events or as recurring tasks. Remember, if you want to attempt to work with cron, you may have to request to do so. On most systems, you must get permission from the system administrator before you can submit job requests to cron. Similarly, on some shared systems, because there is only one crontab file, only the administrator has access to the crontab command.

As mentioned earlier, for commands that need to be executed repeatedly (for example, hourly, daily, or weekly), you can use the crontab command. The crontab command creates a crontab file containing commands and instructions for the cron daemon to execute. You can use the crontab command with many options. If you can master this command, you can automate almost any process for any reason. Let's look at crontab at work.

To use crontab, simple type crontab with an option and then the filename. The filename that you specify will be used for your crontab file. Many times, you can just leave out the option and simply type crontab filename to save that file as your crontab file. An example would be

 >crontab -a filename 

The a option (again, not always needed on some Unix variations) will install the filename as your crontab file. You can also use other options, such as crontab e. This will allow you to edit your crontab file; obviously, if you want to change processes, then this is important to know about and be able to do. You can display your crontab file with the l option. You can use r to remove your crontab file. There are more options for crontab (consult your man pages for additional information), but they are more complex and used for system administration. This overview should be enough to get you started with local automation on your Unix system.

Each entry in a crontab file consists of six fields, specifying (in order) the following information:

 minute(s) hour(s) day(s) month(s) weekday(s) command(s) 

The six fields are separated from one another by spaces or tabs. The first five fields are integer patterns, and the sixth is the command to be executed. Each field is pretty self-explanatory as to what information it requires; the command field obviously names the process itself or the complete sequence of commands to execute. This can also be a shell script. We will cover shell scripts in Lesson 14, "Shell Scripting Fundamentals."

Be Careful with cron If you want to use cron, crontab, or at, it is highly recommended that you check with your Unix system administrator if the system you are working with is not your own. Scheduling processes blindly can affect other users of the system (remember, Unix can serve many people at once), and if you run too many processes or schedule something incorrectly, you may cause problems to your system that will affect not only you but everyone else who uses the system.

You may or may not be alerted if your cron, crontab, or at command creates any problems. If a cron job specified in your crontab entry produces any error messages when it runs, you will get a mail message reporting the errors.


Using at

The at command enables you to run a command once rather than at a repeating interval like cron does. This is useful if there is a processor-intensive task that you want to run after business hours so that other tasks aren't affected.

To schedule a one-time-only task use the at or batch command. To submit a job with the at command, you only need to enter the at command itself and the runtime you want, which is simply the date and time you want the job (process) to execute:

 > at runtime 

Pressing enter here, however, doesn't do anything but enter you into a new mode with a new prompt, most likely resembling the following:

 at> 

What you will see varies among different distributions of Unix, but most times, this is exactly what will appear on your console. At this secondary prompt, you need to just enter the command you would like to run. You can check the status of your command at the system console.

Two Unix Commands for the Price of One! The batch command is similar to at. The syntax for batch is identical to at, except that you do not set an execution time.


So, now that we know how to open it up and get it ready, let's look at the at command in action.

 > at noon at>tar -cf /users/rob/storage storage.tar at>Ctrl-d 

In this example, the user submitted a job that will run at noon the same day if submitted in the morning, or noon the next day if submitted in the afternoon. As well, at will create a tarball of /users/rob/storage directory and call it storage.tar. You can use Ctrl+d to break out of the at process and return to your shell prompt.

Who's the Man? There is a lot more to learn about all the commands we have covered. Continue to use your man pages as often as you can and keep your memorization of these commands to a premium level if you want speed and accuracy every time you come to the console. Unix is intimidating, but as you can see, it's not hard to use and to learn about. Continue to check the man pages when you need help. Some helpful man pages related to the content of this lesson include man crontab, man cron, man at, and man batch.




    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net