24.2 Automating Jobs Using the crontab Command

   

All users who are allowed to use cron can create a file in their home directory using an editor and then submit it for execution using the crontab command. If you are logged in as user boota and have created a file mycrontab in your home directory, the following command will create a file with name boota in the /var/spool/cron/crontabs directory having the same contents as mycrontab .

 crontab mycrontab 

The file contains scheduled programs during which user commands will be executed.

Format of crontab Files

The crontab file for each user is stored in the /var/spool/cron/crontabs directory with the file name being the same as the person's user name. A crontab file for user boota has the name /var/spool/cron/crontabs/boota . A line in the file that starts with a hash symbol ( # ) is considered a comment. An entry for a scheduled program in the file contains six fields. The first five fields are used to specify the time and the last field shows the command that is to be executed at that time. A typical entry in the file is shown in Figure 24-1.

Figure 24-1. A typical entry in a crontab file.

graphics/24fig01.gif

The first field in a line shows the minute of the hour at which the command will be executed. The value of this field may be from 0 to 59. In this figure, the value is 30. The next field shows the hour of the day. Its value may range from 0 to 23. The third field shows the date of the month and its range is 131. The fourth field is the month, and its value can range from 1 to 12. The fifth field shows the day of the week, and its value ranges from 0 to 6, where 0 = Sunday, 1 = Monday, etc.

If multiple values are to be used in a field, these are separated by commas. An entry 1,16 in the date field shows that the command will be executed on the first and sixteenth days of a month. An asterisk shows all values. For example, an asterisk in the month field shows that the command will be executed every month. The command shown in Figure 24-1 copies the /etc/passwd file to /etc/passwd.bak on the first and sixteenth days of every month.

Editing crontab Files

The files used for scheduling cron tasks are edited using the crontab command. With no option, it takes input from stdin and saves it in the user crontab file, overwriting the older file. If you provide a file name on the command line, this file will be copied as your crontab file. For example, the following command will copy a file named mycrontab to /var/spool/cron/crontabs/boota , if you are logged in as user boota .

 crontab mycrontab 

It is a good practice to create a file using the vi editor in your home directory and then submit it for execution using the above command. You can also use this command to list or delete crontab files. Other options that can be used with the crontab command are as follows .

-l Lists the contents of the current crontab file for the user
-e Edits the crontab file for the user
-r Removes the crontab file for a user

Sample crontab File for a Root User

A sample crontab file is shown here that contains three tasks. The first task ( /dailybackup ) is a shell script that is executed once every day at 11:30 p.m. for daily backup. The second one is used to remove core files every hour. The third task is also a shell script that is used to trim log files, and it runs at 11:00 p.m. twice a month on the first and the sixteenth days.

 # crontab file for root # format: # min hour day month dow command 30    23   *   *     *   /dailybackup # remove core every hour 0     *    *   *     *   find / -name core -exec rm {} \; # Trim log files 1  st  . and 16  th  . Of every month at 11 PM. 0     23   1,16  *   *   /trimlog # 

Study Break

Submitting a Job Using the crontab Command

Log in as root and use the crontab -l command to list what schedules are already created. Copy the /var/spool/cron/crontab/root file to your home directory as root.bak . Use the crontab -r command to remove the schedules. Now if you again list the contents of the files, you will see an error message showing that the system is not able to open your file. Create a new file using the crontab -e command and schedule a command ll to be executed every 5 minutes, and redirect its output to a temporary file in your home directory. Wait for some time and you will find output of the command in the file to which you redirected the output. Remove the newly created file and install the old file using the crontab root.bak command. Again list this file to make sure that it is indeed installed.


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

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