Using cron to Manage Periodic JobsEvery Linux and Unix system includes a service called cron that enables users to automatically run specific commands or scripts periodically, according to user-defined timing options. This service offers a range of unique tasksperiodic backups, email status reports, home directory cleanups, and so forth. There are two levels of cron process lists: the systemwide lists and the per-user lists. The systemwide cron process lists should be used only to manage system-oriented processes; for all personal jobs that need to be periodically run, the per-user cron lists should be used instead. Adding Systemwide cron ProcessesSystemwide cron processes are most often used for tasks such as periodic backups, log rotation, and system health monitoring. To create a systemwide cron process, you create a shell script that performs the task you want to accomplish and then, using the cp or mv commands, place it into one of several special directories in /etc:
As is the case with all shell scripts you want to run, scripts that you copy to the /etc/cron.* folders must be marked as executable for them to work properly.
Editing Per-User cron ProcessesIn addition to the systemwide list of cron tasks, which only the root user has permission to edit (because they reside in the /etc folder), cron provides a special way for individual users to manage a personal list of periodic tasks. This is done with the crontab command. To edit your personal lists of cron tasks, enter the crontab -e command at any command line: [you@workstation20 ~]$ crontab -e The vi editor takes over your terminal or console, pointing toward a special file that cron reads to perform the tasks you request. By editing and saving this file, you can cause cron to call any command or script for you on a periodic basis. Each line in the file represents a separate task and must be a list of data fields of the following format: min hour mday month wday command The meaning of each of these fields is described in Table 29.5. Each of them is meaningful: cron uses the values of every field on a specific line to construct the time(s) at which a task should be run.
Enter each of your periodic tasks. After you finish, save the file and exit the vi editor; the new list of jobs is read by cron and carried out at the times you specify until the jobs are removed againmeaning when you enter crontab -e and delete them from the file. Let's examine a few lines from a crontab file for illustration purposes: * * * * * fetchmail 0 15 * * * mcopy ~/database.txt a: 1 0 1 1 * new_year_script The first of these entries calls the fetchmail program described in Chapter 23, "Using the Network at the Command Line," once every minute of every day. In short, this line downloads the user's new mail once per minute. The second of these entries copies a file called database.txt from the user's home directory to the floppy disk in drive a: every day at 3:00 p.m., presumably for backup purposes. The final entry runs a shell script called new_year_script every New Year's Day at 12:01 a.m. |