[Linux] parsing crontab

Source: Internet
Author: User

Linux provides a very powerful and useful command [crontab]

Crontab is used by UNIX and Linux to set up periodic instructions, is a very common Internet technology, many tasks will be set in the crontab loop execution,

If I want Linux timed to execute a script or timed an HTTP request, you can use crontab to implement

as follows, I use cron to send an HTTP request periodically

By default, any user who is not included in the/etc/cron.deny will be able to directly release "Crontab-e" to edit his or her own routine commands! The whole process as mentioned above, will go into the edit screen of VI, and then a work line to edit, after editing the input ": Wq" after the storage Left VI can be!

If you want to run the data synchronization script every 5 minutes, you can also use CRONTAB-E and edit to write in the specified format.

Daytime is the peak, the night is not many users, is the low peak period, we fill the data will occupy a large amount of bandwidth, especially during the day, will affect the normal business, general operations personnel in the server has a large operation when the script will be implemented, and then set in the early hours of the execution of the specified script on it.

The syntax of crontab

crontab [-u username] [-l|-e|-r] options and Parameters:  -u: Only root can do this task, that is, to help other users create/remove crontab work scheduling;  -e: Edit the work contents of crontab-l  : Check out the work of Crontab-R  : Remove all crontab's work, and if you want to remove only one, use-e to edit

Querying the current Scheduled tasks

If you want to delete a crontab task, you can delete a row.

/etc/crontab configuration file Explanation

We can take a look at the Linux/etc/crontab

The first four rows are the environment variables that are used to configure the Crond task to run, the shell variable specifies which shell the system will use, this is bash, and the second line of the path variable specifies the path to the System execution command. The third line of the mailto variable specifies that Crond's task execution information will be emailed to the root user, and if the value of the mailto variable is null, the task execution information is not sent to the user, and the home variable in line fourth specifies the home directory to use when executing the command or script.

Crontab's format explained

In the crontab file created by the user, each line represents a task, each field of each row represents a setting, its format is divided into six fields, the first five is the time setting segment, and the sixth paragraph is the command segment to execute, in the following format:

Minute hour day Month Week command

which

Minute: Represents minutes, which can be any integer from 0 to 59.

Hour: Represents the 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: Represents the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.

Command: The commands to execute can be either system commands or script files that you write yourself.

has been expressed very clearly. Each * represents the meaning of:

Another thing to note here is that there are a few special symbolic meanings.

Special characters Representative meaning
* (asterisk) Mean to accept at any moment, * stands for any time
, (comma)

Represents the meaning of a delimited field

For example: 15,30,45 * * * Command stands for 15 minutes per day, 30 minutes, 45 minutes to execute orders.

-(minus)

Represents a period of time in the range

For example: 7-10 * * Command represents a daily 7-to-10-point 10-minute execution command

/n

That n represents the number, which is the meaning of every n unit interval

For example/5 * * * * Command represents every 5 minutes per day of each month

The principle of crontab

When the user uses the crontab command to create a work schedule, the work is recorded in the/var/spool/cron/, and the account is used as a discriminant! For example, after CSS uses crontab, his work will be recorded in the/VAR/SPOOL/CRON/CSS, but please note, do not use VI directly edit the file, because the input syntax may be wrong, will cause the cron can not be run Oh! In addition, every job that Cron runs will be recorded in the/var/log/cron log file.

The minimum detection limit for the Crond service is minutes, so cron reads the data inside /etc/crontab and /var/spool/cron every minute, so as soon as you finish editing/etc/crontab This file, and store it, then the cron configuration will automatically run,

If you modify the crontab or add the crontab is not immediately executed, then you can use the service Crond restart to restart.

Look at all the cron directories under/etc/.

The special attention is

/etc/cron.deny the user listed in this file does not allow the crontab command to be used

/etc/cron.allow the users listed in this file are allowed to use the crontab command

It is best to back up the crontab command because one of the CRONTAB-E and Crontab-r is written to be deleted, and E and R are very close, so be aware of this and it is easy to accidentally delete crontab this file.

Give a few examples, so that you can easily understand

Instance
Executes every 1 minutes

* * * * * command

/1 * * * * command

Hourly 15th, 30, 45 minutes execution 15,30,45 * * * command
10th, 50 minutes from 7 o'clock to 10. 10,20 7-50 * * command
Every other day from 7 to 11 in the 20th and 40 minutes of execution 20,40 7-11 */1 * command
25th and 50 minutes of every Monday from 7 to 11. 25,50 7-11 * * 1 command
3:30 per day of execution 3 * * command
3:30 per month for 1, 10, 20th 3 1,10,20 * * command
3:30 executions per Saturday, Sunday

3 * * 6,0 command

3 * * 6,7 command

Every 15 minutes from 9 o'clock to 16 every day. /15 9-16 * * command

& Background Execution commands

* * * * * Command &

When a job is run in the foreground, the terminal is occupied by the job, and when the job is run in the background, it does not occupy the terminal. You can use the & command to place jobs in the background. Be careful when running a job in the background: commands that require user interaction are not executed in the background, because your machine will be there to wait.

The output interface can be placed in a certain Wenjiang

2>&1 indicates that all standard output and error outputs will be redirected to a file called Log.file

command>log.file2>&1 &

View crontab execution History under CentOS

Timed tasks were added to the crontab, but it was found that the expected results were not obtained, so it was suspected that Crontab did not perform the corresponding task, which is something we can look at the log

cd/var/logtail-100 Cron

[Linux] parsing crontab

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.