Basic format:
which
Minute: Represents a minute, which can be any integer from 0 to 59.
Hour: Represents an hour, which can be any integer from 0 to 23.
Day: Represents a date, which can be any integer from 1 to 31.
Month: Represents the month, which can be any integer from 1 to 12.
Week: Indicates the day of the week, can be any integer from 0 to 7, where 0 or 7 represent Sunday.
Command: The commands you want to execute, either system commands or script files that you write yourself.
In each of these fields, you can also use the following special characters:
Asterisk (*): represents all possible values, such as the month field if it is an asterisk, indicates that the command operation is performed every month after the constraints of other fields are met.
Comma (,): You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"
Medium Bar (-): You can use the middle bar between integers to represent an integer range, for example, "2-6" means "2,3,4,5,6"
Forward slash (/): You can use a forward slash to specify the interval frequency of the time, for example, "0-23/2" means to perform every two hours. The forward slash can also be used with the asterisk, such as */10, which, if used in the minute field, means that it is executed every 10 minutes.
* * * * command
Time-sharing and Lunar Week command
The 1th column represents minutes 1~59 per minute with * or */1
The 2nd column represents the hour 1~23 (0 for 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
The 5th list of the week 0~6 (0 for Sunday)
6th column the command to run
Some examples of crontab files:
* * * */usr/local/etc/rc.d/lighttpd restart
The above example represents 21:30 restart Apache per night.
4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example represents the 4:45 reboot of Apache for 1, 10, and 22nd per month.
1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows the 1:10 reboot of Apache every Saturday and Sunday.
0,30 18-23 * * * */usr/local/etc/rc.d/lighttpd restart
The example above shows that Apache is restarted every 30 minutes from 18:00 to 23:00 every day.
0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example indicates that 11:00 PM restarts Apache every Saturday.
* */1 * * * */usr/local/etc/rc.d/lighttpd restart
Restart Apache every hour
* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart
From 11 o'clock to 7 in the morning, restart Apache every hour.
0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month with 11 points per Monday to Wednesday restart Apache
0 4 1/USR/LOCAL/ETC/RC.D/LIGHTTPD * Restart
Restart Apache at 4 o ' January 1.
Name: crontab
Use Rights: All users
How to use:
crontab file [-u user]-replaces the current crontab with the specified files.
Crontab-[-u user]-replaces the current crontab with standard input.
crontab-1[user]-lists the user's current crontab.
crontab-e[user]-Edit user's current crontab.
crontab-d[user]-deletes the user's current crontab.
Crontab-c dir-Specifies the directory for crontab.
crontab file format: M H d M D cmd.
M: Minutes (0-59).
H: Hours (0-23).
D: Days (1-31).
M: Month (1-12).
D: Days of the week (0~6,0 for Sunday).
CMD to run the program, the program was sent to SH execution, this shell only user,home,shell these three environment variables
Description
Crontab is used to allow the user to execute a program at a fixed time or at a fixed interval, in other words, a user-like schedule. -u user refers to the setting specified
User's time table, the premise is that you have to have permission (for example, root) to specify other people's schedule. If you do not use-u user, it means that you set the
Set your own schedule.
Parameters:
CRONTAB-E: Execute the text editor to set the schedule, the default text editor is VI, if you want to use another text editor, please set the VISUAL environment variables
To specify the use of that text editor (for example, Setenv VISUAL Joe)
Crontab-r: Deletes the current schedule table
Crontab-l: List the current schedule
crontab file [-u user]-replaces the current crontab with the specified files.
The format of the schedule table is as follows:
F1 F2 F3 f4 f5 program
Where F1 is the minute, F2 represents the Hour, F3 represents the day of the month, the F4 represents the month, and the F5 represents the day of the one week. program says to hold
Line of the program.
When F1 is *, it means that the PROGRAM,F2 is executed every minute for *, and the rest of the program is executed every hour.
When F1 is a-b to be executed from the time of the first a minute to the B minute, the F2 is a-b to be performed from A to a B-hour, and the remainder of the analogy
When F1 is */n, it is executed once every n minutes, F2 is performed once per N-hour intervals for */n, and the rest
When F1 is a, B, C,... The first A, B, C,... Minutes to execute, F2 for a, B, C,... The first is a, B, c ... An hour to execute, and the rest of the analogy
The user can also store all the settings in the file file, using crontab file to set the schedule.
Example:
#每天早上7点执行一次/bin/ls:
0 7 * * */BIN/LS
In December, every day at 6 to 12, every 3 hours of the morning/usr/bin/backup are performed:
0 6-12/3 */usr/bin/backup
Send a letter to Alex@domain.name from Monday to Friday every 5:00:
0 * * 1-5 mail-s "HI" Alex@domain.name </tmp/maildata
Every month at midnight 0:20, 2:20, 4:20 .... Execute echo "haha"
0-23/2 * * * echo "haha"
Attention:
When the program is executed at the time you specify, the system will send you a letter showing the execution of the program, and if you do not wish to receive such a letter, please leave a blank space in each line
After adding >/dev/null 2>&1
Example 2:
#每天早上6点10分
6 * * * Date
#每两个小时
0 */2 * * * Date
#晚上11点到早上8点之间每两个小时, 8 in the morning.
0 23-7/2,8 * * * Date
#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 4 * mon-wed date
#1月份日早上4点
0 4 1, * date
Example
$crontab-L lists the user's current crontab.
Crontab Command Detailed
1. Command format:
Copy code code as follows:
crontab [-u user] File
crontab [-u user] [-e |-l |-r]
2. Command function:
With the crontab command, we can execute the specified system instructions or shell script scripts at a fixed interval of time. The unit of time interval can be any combination of minutes, hours, days, months, weeks, and more. This command is designed to work with periodic log analysis or data backup.
3. Command parameters:
-u User: Used to set a user's crontab service, for example, "-u ixdba" means setting the IXDBA user's crontab service, which is typically run by the root user.
File:file is the name of the command file, which means that file will be crontab as a task list and loaded into crontab. If this file is not specified on the command line, the crontab command accepts the commands typed on the standard input (keyboard) and loads them into crontab.
-E: Edit the contents of a user's crontab file. If you do not specify a user, edit the current user's crontab file.
-L: Displays the contents of a user's crontab file, or displays the contents of the current user's crontab file if no user is specified.
-r: Deletes a user's crontab file from the/var/spool/cron directory, and deletes the current user's crontab file by default if no user is specified.
-I: Give a confirmation prompt when deleting a user's crontab file.
4. Common methods:
1). Create a new crontab file
One of the first things to do before considering submitting a crontab file to a cron process is to set up the environment variable editor. The cron process depends on it to determine which editor to use to edit the crontab file. 9 9 of UNIX and Linux users use VI, if you do the same, then you edit the $ home directory. Profile file in which to add one line:
Editor=vi; Export EDITOR
Then save and exit. You may wish to create a file named <user> Cron, where <user> is the user name, for example, Davecron. Add the following content to the file.
Copy code code as follows:
# (Put your own initials) echo the date to the console every
# 15minutes between 6pm and 6am
0,15,30,45 18-06 * * * */bin/echo ' date ' >/dev/console
Save and exit. Make sure that the first 5 fields are separated by spaces.
In the example above, the system will output the current time to the console every 1 5 minutes. If the system crashes or hangs, you can see at what time the system stopped working from the last time it was displayed. In some systems, using tty1 to represent the console, the above example can be modified according to the actual situation. To submit the crontab file you just created, you can use this newly created file as a parameter to the cron command:
Copy code code as follows:
$ crontab Davecron
The file is now submitted to the Cron process, which will run every 1 to 5 minutes.
Also, a copy of the newly created file has been placed in the/var/spool/cron directory, and the filename is the username (that is, Dave).
2). List crontab Files
To list crontab files, you can use:
Copy code code as follows:
$ crontab-l
0,15,30,45,18-06 * * */bin/echo ' date ' > dev/tty1
You will see something similar to the above. You can use this method to make a backup of the crontab file in the $ H O M e directory:
Copy code code as follows:
$ crontab-l > $HOME/mycron
In this way, once you accidentally delete the crontab file, you can use the method described in the previous section to recover quickly.
3). Edit Crontab File
If you want to add, delete, or edit entries in the Crontab file, and the E D i to r environment variable is set to V I, you can edit the crontab file with V I, and the corresponding command is:
Copy code code as follows:
$ crontab-e
You can modify the crontab file as you would edit any other file using V I and exit. If you modify some entries or add new entries, the C R o N will perform the necessary integrity checks on the file when you save it. If one of the fields has a value that is outside the allowable range, it prompts you.
When we edit the crontab file, we may add a new entry. For example, add the following article:
Copy code code as follows:
# Dt:delete Core Files,at 3.30am on 1,7,14,21,26,26 days of each month
3 1,7,14,21,26 * */bin/find-name "core"-exec rm {} \;
Now save and exit. It's a good idea to add a comment above each entry in the crontab file so that you know its function, uptime, and more importantly, who knows which user's job.
Now let's list all of its information using the CRONTAB-L command we talked about earlier:
Copy code code as follows:
$ crontab-l
# (Crondave installed on Tue 4 13:07:43 1999)
# Dt:ech The date to the console every minites
0,15,30,45 18-06 * * * */bin/echo ' date ' >/dev/tty1
# Dt:delete Core Files,at 3.30am on 1,7,14,21,26,26 days of each month
3 1,7,14,21,26 * */bin/find-name "core"-exec rm {} \;
4). Delete crontab file
To delete a crontab file, you can use:
Copy code code as follows:
$ crontab-r
5. Recovery of Lost crontab files
If you accidentally delete the crontab file, assuming you have a backup in your own $ H O M directory, you can copy it to/var/spool/cron/<username>, where <username> is the user name. If the copy cannot be completed due to permission issues, you can use:
Copy code code as follows:
$ crontab <filename>
Where,<filename> is the file name of your replica in the $ H O M e directory.
I suggest that you save a copy of the file in your own $ H O M e directory. I have had a similar experience, several times mistakenly deleted the crontab file (because the R key is close to the right of the E key). This is why some system documents do not recommend editing the crontab file directly, but instead edit a copy of the file, and then resubmit the new file.
Some crontab variants are a bit weird, so be careful when using the crontab command. If you omit any of the options, crontab may open an empty file, or it might look like an empty file. Then click the Delete key to exit, do not press <CTRL-D>, or you will lose crontab file.