How to Use the crontab command in the CentOS command list

Source: Internet
Author: User

How to Use the crontab command in the CentOS command list

Basic Format:
Where:
Minute: minute. It can be any integer from 0 to 59.
Hour: indicates the hour, which can be any integer from 0 to 23.
Day: indicates the date, which can be any integer from 1 to 31.
Month: represents the month, which can be any integer from 1 to 12.
Week: the day of the week. It can be any integer from 0 to 7. Here 0 or 7 represents Sunday.
Command: the command to be executed. It can be a system command or a script file compiled by yourself.

You can also use the following special characters in the preceding fields:
Asterisk (*): represents all possible values. For example, if the month field is an asterisk, this command is executed every month after the conditions of other fields are met.
Comma (,): values separated by commas (,) can be used to specify a list range, for example"
Middle bars (-): You can use the middle bars between Integers to represent an integer range. For example, "2-6" indicates "2, 3, 4, 5, 6"
Forward slash (/): You can use a forward slash to specify the interval of time. For example, "0-23/2" indicates that execution is performed every two hours. At the same time, the forward slash can be used with the star number, for example, */10. If it is used in the minute field, it indicates that the execution is performed every ten minutes.

* *** Command

Hour, day, month, and week commands
The 1st column indicates minute 1 ~ 59. Each minute is represented by * or */1.
The first column indicates the hour 1 ~ 23 (0 indicates 0 points)
The 3rd column indicates the date 1 ~ 31
The 4th column indicates the month 1 ~ 12
The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)
6th columns of commands to run
Some examples of crontab files:
30 21 ***/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates restarting apache at every night.
45 4, 10, 22 **/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted at on the 1st, 10th, and 22th every month.
10 1 ** 6, 0/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted at every Saturday and Sunday.
0, 30 18-23 ***/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted every 30 minutes between and every day.
0 23 ** 6/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted at every Saturday.
**/1 ***/usr/local/etc/rc. d/lighttpd restart
Restart apache every hour
* 23-7/1 ***/usr/local/etc/rc. d/lighttpd restart
Restart apache every hour between PM and PM.
0 11 4 * mon-wed/usr/local/etc/rc. d/lighttpd restart
Restart apache on November 4 and every Monday to Wednesday.
0 4 1 jan */usr/local/etc/rc. d/lighttpd restart
Restart apache at on January 1, January 1
Name: crontab
Permission: All Users
Usage:
Crontab file [-u user]-replace the current crontab with the specified file.
Crontab-[-u user]-replace the current crontab with the standard input.
Crontab-1 [user]-list the current crontab of the user.
Crontab-e [user]-edit the current crontab of the user.
Crontab-d [user]-delete the current crontab of the user.
Crontab-c dir-specifies the crontab directory.
Crontab file format: m h d m d cmd.
M: minute (0-59 ).
H: hour (0-23 ).
D: Day (1-31 ).
M: Month (1-12 ).
D: days in a week (0 ~ 6, 0 is Sunday ).
The program to run in cmd is sent to sh for execution. This shell only contains three environment variables: USER, HOME, and SHELL.
Note:
Crontab is used to allow users to execute programs at a fixed time or interval. In other words, it is similar to the user's time table. -U user is used to specify
The premise is that you must have the permission (for example, root) to specify the time table of another user. If-u user is not used, it indicates
Set your own time table.
Parameters:
Crontab-e: run the text editor to set the time table. The preset text editor is VI. If you want to use another text editor, set the VISUAL environment variable first.
To specify the Text Editor (for example, setenv VISUAL joe)
Crontab-r: Delete the current time table
Crontab-l: to list the current time table
Crontab file [-u user]-replace the current crontab with the specified file.
The time table format is as follows:
F1 f2 f3 f4 f5 program
F1 indicates the minute, f2 indicates the hour, f3 indicates the day of the month, f4 indicates the month, and f5 indicates the day of the week. Program indicates to execute
.
When f1 is *, the program is executed every minute. When f2 is *, the program is executed every hour, and so on.
When f1 is a-B, it indicates that execution is performed from the minute a to the minute B. When f2 is a-B, it indicates that execution is performed from the hour a to the hour B, and so on
When f1 is */n, it indicates execution is performed every n minutes. If f2 is */n, it indicates execution is performed every n hours, and so on.
When f1 is a, B, c ,... a, B, c ,... execute in minutes. f2 is a, B, c ,... a, B, c... execution in hours, and so on
You can also store all settings in the file first, and use crontab file to set the time table.
Example:
# Run/bin/ls at every morning:
0 7 ***/bin/ls
During October 11, December, execute/usr/bin/backup every three hours from to every day:
0 6-12/3*12 */usr/bin/backup
From Monday to Friday, send a letter to alex@domain.name at pm:
0 17 ** 1-5 mail-s "hi" alex@domain.name </tmp/maildata
Execute echo "haha" at midnight, 00:20, and 02:20 every month"
20 0-23/2 *** echo "haha"
Note:
When the program is executed at the specified time, the system will send you a letter showing the program execution content. If you do not want to receive such a letter, please leave each row Blank
Add>/dev/null 2> & 1.
Example 2:
#06:10 every morning
10 6 **** date
# Every two hours
0 */2 * date
# Every two hours from PM to am, am
0 23-7/2, 8 **** date
# Am on the 4th day of each month and from Monday to Wednesday of each week
0 11 4 * mon-wed date
# A.m. of July
0 4 1 jan * date
Example
$ Crontab-l list the current crontab of a user.

Crontab command details
1. Command Format:
Copy the Code as follows:
Crontab [-u user] file
Crontab [-u user] [-e |-l |-r]
2. command functions:
Through the crontab command, we can execute the specified system command or shell script at a fixed interval. The time interval can be any combination of minutes, hours, days, months, weeks, and above. This command includes non-permanent and periodic log analysis or data backup.
3. command parameters:
-U user: used to set the crontab service for a user. For example, "-u ixdba" indicates to set the crontab service for the ixdba user. This parameter is generally run by the root user.
File: file is the name of the command file. It indicates that file is used as the task list file of crontab and is loaded into crontab. If this file is not specified in the command line, the crontab command accepts the commands typed on the standard input (keyboard) and loads them into the crontab.
-E: edit the contents of a user's crontab file. If no user is specified, the crontab file of the current user is edited.
-L: displays the contents of a user's crontab file. If no user is specified, the contents of the current user's crontab file are displayed.
-R: deletes the crontab file of a user from the/var/spool/cron directory. If no user is specified, the crontab file of the current user is deleted by default.
-I: A confirmation prompt is displayed when the user's crontab file is deleted.
4. Common Methods:
1) Create a New crontab file
Before submitting a crontab file to the cron process, you must set the environment variable EDITOR. The cron process determines which editor to use to edit the crontab file. 9 9% of UNIX and LINUX users use vi. If so, edit the. profile file in the $ HOME directory and add the following line to it:
EDITOR = vi; export EDITOR
Save and exit. Create a file named <user> cron, where <user> is the user name, for example, davecron. Add the following content to the file.
Copy the Code as follows:
# (Put your own initials here) echo the date to the console every
#15 minutes between 6 and 6 am
0, 15, 30, 45 18-06 ***/bin/echo 'date'>/dev/console
Save and exit. Make sure that the first five fields are separated by spaces.
In the preceding example, the system outputs the current time to the console every 5 minutes. If the system crashes or hangs, you can see at the last displayed time when the system stops working. In some systems, tty1 is used to represent the console. You can modify the preceding example based on the actual situation. To submit the crontab file you just created, you can use the newly created file as a parameter of the cron command:
Copy the Code as follows:
$ Crontab davecron
Now the file has been submitted to the cron process and will run every 5 minutes.
At the same time, a copy of the new file has been stored in the/var/spool/cron directory, and the file name is the user name (dave ).
2) List crontab Files
To list crontab files, you can use:
Copy the 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 back up the crontab file in the $ h o m e directory:
Copy the Code as follows:
$ Crontab-l> $ HOME/mycron
In this way, once the crontab file is accidentally deleted, you can use the method described in the previous section to quickly restore it.
3) edit the crontab file
If you want TO add, delete, or edit entries in the crontab file, and set the environment variable e d I to r to v I, you can use v I TO edit the crontab file, the corresponding command is:
Copy the Code as follows:
$ Crontab-e
You can modify the crontab file and exit as you can edit any other file using v I. If some entries are modified or new entries are added, c r o n performs the necessary integrity check when saving the file. If a field contains a value out of the permitted range, it will prompt you.
We may add new entries when editing the crontab file. For example, add the following one:
Copy the Code as follows:
# DT: delete core files, at 3.30am on, days of each month
30 3, 26 **/bin/find-name "core'-exec rm {}\;
Save and exit now. It is best to add a comment to each entry in the crontab file so that you can know its function and running time. More importantly, you can know which user's job this is.
Now let's use the previous crontab-l command to list all its information:
Copy the Code as follows:
$ Crontab-l
# (Crondave installed on Tue May 4 13:07:43 1999)
# DT: ech the date to the console every 30 minites
0, 15, 30, 45 18-06 ***/bin/echo 'date'>/dev/tty1
# DT: delete core files, at 3.30am on, days of each month
30 3, 26 **/bin/find-name "core'-exec rm {}\;
4). Delete the crontab file.
To delete the crontab file, you can use:
Copy the Code as follows:
$ Crontab-r
5) restore the lost crontab file
If you accidentally delete the crontab file by mistake, assume that you have a backup under your $ h o m e 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 the Code as follows:
$ Crontab <filename>
<Filename> indicates the file name of the copy in the $ h o m e directory.
I suggest you save a copy of the file in your $ H O M E directory. I have had a similar experience and deleted the crontab file several times by mistake (because the r key is next to the right of the e key ). This is why it is recommended that you do not directly edit the crontab file in some system documents, but edit a copy of the file and resubmit the new file.
Some crontab variants are somewhat weird, so be careful when using the crontab command. If you omit any options, crontab may open an empty file or it looks like an empty file. Press delete to exit. Do not press <Ctrl-D>. Otherwise, the crontab file will be lost.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.