The at and cron CommandsThus 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.
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 cronAs 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."
Using atThe 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.
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.
|