March 25, 2011, 9:30 a.m.
posted by homene
Using cron to Manage Periodic Jobs
Every 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 Processes
Systemwide 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:
Editing Per-User cron Processes
In 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:
[[email protected] ~]$ 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.
min hour mday month wday command
The meaning of each of these fields is described in Figure. 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.
* * * * * 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.