Implement timed tasks with Crontab commands under Linux

Source: Internet
Author: User

I. Introduction of Crond

Crond is a daemon that is used to periodically perform certain tasks or wait for certain events under Linux, similar to Scheduled tasks under Windows, when the operating system is installed, the Service tool is installed by default and the Crond process is started automatically. The Crond process periodically checks to see if there is a task to perform and automatically executes the task if there are tasks to perform.

The task scheduling under Linux is divided into two categories, system task scheduling and user task scheduling.

System task scheduling: The work to be performed by the system periodically, such as writing cache data to hard disk, log cleanup, etc. In the/etc directory there is a crontab file, this is the System Task Scheduler configuration file.

Two. Meaning of the crontab file

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.

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, the command action is executed monthly 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"

Middle Bar (-): An integer range can be represented by a middle bar between integers, such as "2-6" for "2,3,4,5,6"

Forward slash (/): You can specify the interval frequency of the time with a forward slash, such as "0-23/2", which is performed every two hours. A forward slash can be used with asterisks, such as */10, if used in the minute field, which means that it executes every 10 minutes.

Three. Example

Example 1: command is executed every 1 minutes

Command:

* * * * * command

Example 2:3rd and 15 minutes per hour of execution

Command:

3,15 * * * command

Example 3: Execution at 3rd and 15 minutes from 8 o'clock in the morning to 11.

Command:

3,15 8-11 * * command

Example 4:3rd and 15 minutes of every two-day 8 o'clock in the morning to 11-point execution

Command:

3,15 8-11 */2 * command

Example 5:3rd and 15 minutes of each Monday from 8 o'clock in the morning to 11.

Command:

3,15 8-11 * * 1 command

Example 6:21:30 restart of SMB per night

Command:

* * * * */ETC/INIT.D/SMB restart

Example 7:4:45 restart SMB per month for 1, 10, 22nd

Command:

4 1,10,22 * */ETC/INIT.D/SMB restart

Example 8:1:10 restart SMB per Saturday, Sunday

Command:

1 * * 6,0/ETC/INIT.D/SMB restart

Example 9: Restart SMB every 30 minutes from 18:00 to 23:00 daily

Command:

0,30 18-23 * * */ETC/INIT.D/SMB restart

Example 10: Every Saturday night at 11:00am restart SMB

Command:

0 * * 6/ETC/INIT.D/SMB restart

Example 11: Restart SMB every hour

Command:

* */1 * * * */ETC/INIT.D/SMB restart

Example 12: Restart SMB every hour from 11 o'clock to 7 in the morning

Command:

* 23-7/1 * * * */ETC/INIT.D/SMB restart

Example 13:4th per month with 11 points per Monday to Wednesday restart SMB

Command:

0 4 * MON-WED/ETC/INIT.D/SMB restart

Example 14:4-point restart of SMB on January 1

Command:

0 4 1 Jan */ETC/INIT.D/SMB restart

Example 15: Execution of scripts within/etc/cron.hourly directory per hour

Command:

* * * * * Root run-parts/etc/cron.hourly

Implement timed tasks with Crontab commands under Linux

Related Article

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.