Crontab command
The crontab command is used to install, delete, or list tables used to drive cron background processes. That is to say, the user puts the command sequence to be executed into the crontab file for execution. Each user can have their own crontab file. The following describes how to create a crontab file.
Crontab files under/var/spool/cron cannot be directly created or modified. The crontab file is obtained through the crontab command. Assume that you have a username of foxy, and you need to create your own crontab file. First, you can use any text editor to create a new file, and then write the commands to be run and the time to be periodically executed to it.
Then save the disk and exit. Assume the file is/tmp/test. cron. Then, use the crontab command to install the file and make it the user's crontab file. Type:
Crontab test. cron
A crontab file is created. You can go to the/var/spool/cron directory and check that there is an additional foxy file. This file is the required crontab file. You can use the more command to view the file content and find that there are three lines of information in the file header:
# Do not edit this file-edit the master and reinstall.
# (Test. cron installed on Mon Feb 22 14:20:20 1999)
# (Cron version -- $ ID: crontab. C, V 2.13 1994/01/17 03:20:37 vivie exp $)
The approximate meaning is:
# Do not edit this file-if you need to change it, edit the source file and reinstall it.
# Test. Cron File Installation time: 14: 20: 20 02/22/1999
If you want to change the command content, you need to re-edit the original file and then use the crontab command to install it.
Users who can use the crontab command are limited. If/etc/cron. if the Allow file exists, only the listed users can use this command. If the file does not exist but cron. if the deny file exists, only users not listed in the file can use the crontab command. If neither file exists, it depends on the setting of some parameters, it is possible that only the super user is allowed to use this command, or that all users can use this command.
The syntax format of the crontab command is as follows:
Crontab [-u user] File
Crontab [-u user] {-L |-r |-e}
The first format is used to install a new crontab file and install the file referred to by the fade away variable ile. If the "-" symbol is used as the file name, it means the standard input is used as the installation source.
-U if this option is used, that is, the crontab file of the specified user will be modified. If this option is not specified, crontab is the * Author's crontab by default, that is, the crontab file of the user executing the crontab command will be modified. However, if you use the su command and then use the crontab command, there may be confusion. Therefore, if the su command is used, it is best to use the-u option to specify the user's crontab file.
-L display the current crontab on the standard output.
-R: Delete the current crontab file.
-E use the editor referred to by visual or editor environment variables to edit the current crontab file. After editing is completed, the edited files are automatically installed.
[Example 7]
# Crontab-L # list the current crontab of a user.
10 6 **** date
0 */2 * Date
0 23-7/2, 8 **** date
#
In the crontab file, enter the command and time to be executed. Each line in this file contains six fields, the first five of which specify the time when the command is executed, and the last field is the command to be executed. Each domain is separated by spaces or tabs. The format is as follows:
Minute hour day-of-month-of-year Day-of-week commands
The first item is the minute, the second item is the hour, the third item is the day of the month, the fourth item is the month of the year, and the fifth item is the day of the week, the sixth item is the command to be executed. These items cannot be blank and must be filled in. If you do not need to specify several items, you can use * instead. Because * is a unified character and can replace any character, it can be considered as any time, that is, this item is ignored. The valid range of each item is given in Table 4-1.
Table 4-1 valid time range
Time
Valid Value
Minute
00-59
Hour
00-23, where is
Day-of-month
01-31
Month-of-year
01-12
Day-of-week
0-6, of which Sunday is 0
In this way, you can write unlimited lines to the crontab file to complete unlimited commands. All commands and symbols that can be written in the command line can be written in the Command domain, and other time domains can be listed, that is, many time values can be written in the domain, if any of these time values is met, execute the command, and use commas to separate each two time values.
In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "*/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers. Examples:
Every morning
0 6 *** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox.
Every two hours
0 */2 *** echo "have a break now.">/tmp/test.txt
Every two hours from PM to am, am
0 23-7/2, 8 *** echo "have a good dream :)">/tmp/test.txt
Am from Monday 4 to Wednesday every week
0 11 4*1-3 command line
Am, January 1, January 1
0 4 1 1 * command line
Shell =/bin/bash
Path =/sbin:/bin:/usr/sbin:/usr/bin
Mailto = root // if an error occurs or data is output, the data is sent to this account as an email.
Home = // path of the user running. The root directory is used here.
# Run-Parts
01 *** root run-parts/etc/cron. Hourly // execute the script in/etc/cron. Hourly every hour
02 4 *** root run-parts/etc/cron. daily // run the script in/etc/cron. daily every day.
22 4 ** 0 root run-parts/etc/cron. Weekly // execute the script in/etc/cron. Weekly every week
42 4 1 ** root run-parts/etc/cron. Monthly // run the script in/etc/cron. Monthly every month.
Note the "run-parts" parameter. If this parameter is removed, you can write a script name to be run, instead of the folder name.
[Example 8]
5, 15, 25, 35, 45, 55, 16, 17, 18 *** command
This indicates the calendar month of any day, in fact, the command is executed every day at four o'clock P.M., 15 min, 25 min, 35 min, 45 min, and 55 min.
[Example 9] The system enters the maintenance status and restarts at every Monday, Wednesday, or Friday. The following fields should be written to the crontab file:
00 15 ** 1, 3, 5 shutdown-R + 5
Then, save the file as Foxy. cron, and then type crontab Foxy. cron to install the file.
[Example 10] execute the innd/bbslin command in the user directory at 10 or 40 points per hour:
10, 40 * innd/bbslink
[Example 11] Run the bin/account command in the user directory every hour:
1 * bin/account
[Example 12] execute the following two commands in the user directory at 03:20 every morning (each command is separated ):
20 3 *** (/bin/Rm-F expire. ls logins. Bad; bin/expire $ # @ 62; expire.1st)
[Example 13] Run the/bin/Rm-F expire.1stcommand at 03:12 and 03:55 on July 4 and July 9, and set the result to the end of the mm.txt file (the mm.txt file is located in the user's directory ).
3 4-9 */bin/Rm-F expire.1stre%@62%%%%%@62%mm.txt
[Example 14] Let's look at a Super User's crontab file:
# Run the 'atrun' program every minutes
# This runs anything that's due to run from 'at'. See man 'at' or 'atrun '.
, 20, 25, 30, 35, 40, 45, 50, 55 ***/usr/lib/Atrun
40 7 * updatedb
, */Bin/Sync