This article introduces how to use crontab in linux and how to use crontab to implement scheduled tasks. This scheduled task is a bit like a scheduled task in windows.
1. Role
You can use the crontab command to modify the crontab configuration file.
.
2. Format
The Code is as follows: |
Copy code |
Crontab [-u user] File Crontab [-u user] {-l |-r |-e} |
3. Main Parameters
-E: run the text editor to set the time table. The text editor is vi.
-R: Delete the current time table.
-L: list the current time table.
The format of the crontab file is m h d m d cmd ". M indicates the minute (0 ~ 59), H Represents the hour (
0 ~ 23), D represents the Day (1 ~ 31), m represents the month (1 ~ 12), d indicates the day in a week (0 ~ 6, 0 is a star
Period ). Cmd indicates the program to run. It is sent to sh for execution. This Shell only includes USER, HOME, and SHELL.
Three environment variables.
4. Description
Compared with the at command, the crontab command is suitable for tasks with a fixed cycle.
5. Application Instance
Set a scheduled and scheduled system prompt:
[Cao @ www cao] # crontab-e
The system opens a vi editor.
If you enter the following content: 35 17 ** 5 wall "Tomorrow is Saturday I will go CS ",
In this way, a terminal will pop up at every Friday to remind you to play CS on Saturday!
Use the crontab command to scan viruses on a daily basis
We have introduced a simple crontab command operation. Here we will look at some more important operations.
The Code is as follows: |
Copy code |
30 21 ***/opt/lampp restart # The example above indicates restarting lampp at every night. 45 4, 10, 22 **/opt/lampp restart # The example above indicates restarting lampp at on the first, 10th, and 22th of every month. 10 1 ** 6, 0/opt/lampp restart # The preceding example indicates restarting lampp at every Saturday and Sunday. 0, 30 18-23 ***/opt/lampp restart # The preceding example indicates restarting lampp every 30 minutes between and every day. 0 23 ** 6/opt/lampp restart # The example above indicates restarting lampp at every Saturday. **/1 ***/opt/lampp restart # Restart lampp every hour * 23-7/1 ***/opt/lampp restart # Restart lampp every hour between PM and PM 0 11 4 * mon-wed/opt/lampp restart # Restart lampp at from Monday to Wednesday on the 4th of each month 0 4 1 jan */opt/lampp restart # Restart lampp at on January 1, January 1 |
/Sbin/service crond start // start the service
/Sbin/service crond stop // close the service
/Sbin/service crond restart // restart the service
/Sbin/service crond reload // reload the configuration
View, edit, and delete
Cron saves the command line in the crontab (cron table) file, which is usually in the/etc directory. Each system user can have their own crontab (under/var/spool/cron ). To view the current user's crontab, enter crontab-l. to edit crontab, enter crontab-e. To delete crontab, enter crontab-r. If the current identity is root, to View/Edit/delete/crontab of a user, you only need to add-u USERNAME (such as crontab-e-u USERNAME) after the corresponding command. The Default editor of the crontab file is vi. You can enter export VISUAL = 'editor' to change the default editor.
The cron service reads not only all files in the/var/spool/cron directory once per minute, but also the/etc/crontab file once. Configuring this file also allows cron to execute tasks. The crontab command is used to configure user-level tasks, and editing the/etc/crontab file is used to configure system-level tasks.
Syntax description
The following is an example of two cron statements (in the/etc/crontab file ). The former is used to back up the/etc directory in the evening, and the latter runs the Analog program to process server statistics.
12 3 * root tar czf/usr/local/backups/daily/etc.tar.gz/etc>/dev/null 2> & 152 5 * root/usr/local/ src/analog-5.32-lh/analog>/dev/null 2> & 1 the following fields and fields in the cron statement:
Field description
1 minute (0-59)
2 hours (2-24)
3 date (1-31)
January (1-12; or Jan, Feb, etc)
The day of the week (0-6 or 0 is Sunday, or Sun or Mon)
6 User Name (this user identity is used when executing commands)
7. command to be executed (PATH)
Now let's look at the first line:
12 3 *** root tar czf/usr/local/backups/daily/etc.tar.gz/etc>/dev/null 2> & 1 this statement will be executed at 03:12 every day () run the command tar czf/usr/local/backups/daily/etc.tar.gz/etc.> /Dev/null 2> & 1 indicates that all the standard output is sent to/dev/null (the recycle bin of linux), and the standard error output (2) is sent to and the standard output (1) similarly (/dev/null ). Running this command will not produce any output.
This statement can be a little more complex:
30 15 13 6 1 * root tar czf/usr/local/backups/daily/etc.tar.gz/etc>/dev/null 2> & 1 it will run tar at on Monday, June 13 czf/usr/local/backups/daily/etc.tar.gz/etc command.
The following statement can achieve the same effect:
30 15 13 Jun Mon * root tar czf/usr/local/backups/daily/etc.tar.gz/etc>/dev/null 2> & 1 if you want run a program in 15th minutes of the hour, you can use:
15 * joey/usr/bin/somecommand>/dev/null 2> & 1 where the asterisk (*) is a wildcard, indicating that cron will ignore this field.
If you want to run a program every two hours, you can use */2 in the hour field. It will be ...... Run. The statement is as follows:
0 */2 * joey/usr/bin/somecommand>/dev/null 2> & 1cron statements can also use commas (,) to specify multiple times. For example, if you want to run a program at 15 or 30 minutes per hour, you can use the following values in the minute field:
15, 30 * joey/usr/bin/somecommand>/dev/null 2> & 1 if you want to be in the first week of each month (I .e. between 1 and 7) to run a program at a specified time every day, you can use 1-7 in the date field:
15, 30 */2 1-7 ** joey/usr/bin/somecommand>/dev/null 2> & 1 this statement will be executed on the 15th day of every two hours of every month minute and 30 minute, ...... 22: 15,22: 30) run the/usr/bin/somecommand command.
If you want to execute a script set at every day, you can put all the scripts to be executed in a directory (such as/home/username/cron). You can use:
18 16 * root run-parts/home/username/cron>/dev/null 2> & 1 if you want to save the output result of a program, replace >>>/dev/null 2> & 1 with >>>/home/user/somecommand. log 2> & 1.
The crontab command is used to regularly execute specified commands. Similar to the "Task Plan" in Windows, it is usually used for repetitive work.
Linux users only need to add the command sequence to the crontab file, and the operating system will execute the command sequence according to the time configured by the user.
Before adding commands to the crontab file, you need to check whether the crontab service is started and whether the crontab service is automatically started upon startup:
View crontab service status: service crond status
Start the crontab service manually: service crond start
Check whether the crontab service is set to start at startup. Run the following command: ntsysv.
Open the service configuration and check whether [] before the crond service is selected (enter * to select). If not, select the service.
Run crontab-l to view the task plans configured by the current user.
Run the "crontab-e" command to edit the crontab file.
You can also create a crontab configuration file without using the "crontab-e" command, and write a command sequence in any other text editor, for example, the file name is "abc. cron, and then use the command: "crontab abc. cron "to add the command sequence to crontab.
By default, all users of the system can use the crond service. To restrict the use of the crond service, use the configuration file/etc/cron. allow and/etc/cron. deny, Wang wenshengyi, cron. allow is a user list that allows the use of the crond service, cron. the opposite is true for users in deny.
The syntax format of the crontab command is as follows:
Format 1: crontab [-u user] file
This command is used to add a new crontab file.
-U if this option is used, the crontab file of the specified user will be modified. Otherwise, the crontab file of the user who runs the crontab command will be modified.
Format 2: crontab [-u user] [-e |-l |-r]
-L display the current crontab on the standard output.
-R: Delete the current crontab file.
-E use the system editor to edit the current crontab file. After editing, the file is automatically installed. This method is convenient and takes effect immediately after modification.
This is the focus for a long time. The command format in the crontab file is as follows:
Minute hour day-of-month-of-year day-of-week commands
That is, the "hour, day, month, and week command" is a total of 6 segments. The first 5 segments are time periods, and the last segment is the command you want to execute on a regular basis. Use spaces or tabs to separate segments.
Let's take a look at the value range of the first five paragraphs, namely, date and time:
1, minute [00-59]
2, hour [00-23]
3, [01-31]
4. month [01-12]
5, week [0-6]; Note: Here 0 represents Sunday
OK. Let's take a simple example. get up at half past six every morning:
30 06 *** echo "Get Up Now! ">/Tmp/test.txt
Add the preceding command to the crontab file. At half past six A.M. every day, the system adds a "Get Up Now!" line to the/tmp/test.txt file !".
The preceding command uses the special symbol "*", which is similar to "*" in the SQL query statement "SELECT * FROM TAB, that is, all numbers within the range of the current segment. For example, "*" in section 3rd represents [01-31, "*" in section 4th represents [01-12] months.
In addition to "*", special symbols include "/", "-", and ",":
/Indicates the meaning of "every". For example, "/5" indicates every five units;
-Indicates a range. For example, "[1-10]" indicates a range from 1 to 10;
Used to represent discrete numbers, such as "5, 15, 25"
For example, the command: [0-59]/5 23 ***/sbin/shutdown-h now
It indicates that shutdown is attempted every five minutes starting at every night to help you develop a good habit of going to bed early and getting up early. After the machine is turned off, if you try to start the machine, it will be turned off again at a multiple of 5, like the shock wave virus in Windows 2000.
, *** Echo "10 minutes has passed! ">/Tmp/test.txt
Every hour's 00 minutes, 10 minutes, 20 minutes, 30 minutes, 40 minutes, 50 minutes to the file "/tmp/test.txt" Write a line and ten minutes passed