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.
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.
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.
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 |
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 BreakSubmitting a Job Using the crontab Command
|
Top |