Use the crontab command in Linux to enable custom timed task instances _linux

Source: Internet
Author: User

The regular execution of Linux is mainly done by adding custom plans to the Crontab file, which is slightly more complex than Windows (because there is no graphical interface), but it is not very complex, but it is not very complicated.

A Brief introduction

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

System task scheduling: The system needs to perform periodic tasks, such as restart, log cleanup, and so on, its configuration file is:/etc/crontab

User Task scheduling: a task that a user needs to perform on a regular basis. Users can use the crontab command to configure their scheduled tasks. All user-configured scheduled tasks are stored in the/var/spool/cron/directory and have the same file name as the user name. For example, all scheduled tasks for root users are saved in/var/spool/cron/root files

The meaning of two crontab documents

All user crontab files are stored in the/var/spool/cron/directory, named after the username. Each of these lines represents a task, each of which represents a single configuration, with a total of six fields in its format, the first five being the time setting segment, and the sixth paragraph being the command segment to be executed, in the following format:

    • Minute hour day Month Week command
    • Minute: Represents minutes, can be any integer from 0 to 59
    • Hour: Represents an hour, 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, can be any integer from 1 to 12
    • Week: For weeks, can be any integer from 0 to 7, where 0 or 7 represent Sundays
    • Command: Indicates the commands you need 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:

* represents all possible values, such as: If the month field is an asterisk, the command is executed every month after the constraints of other fields are met

, a comma-separated value represents a list of scopes, for example: if the value of the minute field is "1,3,15", the command is executed for the 1th, 3rd, and 15 minutes of the hour.

– A range of integers can be represented by integers-for example, if the value of the day field is "2-6", the command is executed 6 days on the 2nd of the Month

/You can use a slash to indicate how often a command is executed, for example: if the value of the minute field is "*/10", the command is executed every 10 minutes

31 instance of a timed task

A server synchronizes time to 192.168.1.2 every 10 minutes:

[Root@web01 cron]# Vim/var/spool/cron/root

Add the following:

0-59/10 * * * */usr/sbin/ntpdate-s 192.168.1.2

As you can see from the above introduction, this code means: from 0 to 59 points in this time period every 10 minutes to execute the time synchronization command, its time synchronization server is 192.168.1.2

Four other examples

Example 1: Execute command once every 1 minutes: * * * * * command

Example 2:3rd and 15 minutes per hour executing commands: 3,15 * * * command

Example 3:3rd and 15 minutes from 8 o'clock in the morning to 11 execute command: 3,15 8-11 * *

Example 4: Every two days from 8 o'clock in the morning to 11 points of the 3rd and 15 minutes to execute the command: 3,15 8-11 */2 * * command

Example 5: Each Monday from 8 o'clock in the morning to 11 points of the 3rd and 15 minutes to execute the command: 3,15 8-11 * 1 command

Example 6:21:30 restart SMB command per night: * * * */ETC/INIT.D/SMB restart

Example 7:4:45 restart SMB Command 1, 10, 22nd per month: 4 1,10,22 * */ETC/INIT.D/SMB restart

Example 8:1:10 restart SMB commands per Saturday, Sunday: 1 * * 6,0/ETC/INIT.D/SMB restart

Example 9: Restart the SMB command every 30 minutes from 18:00 to 23:00 every day: 0,30 18-23 * * * */ETC/INIT.D/SMB restart

Example 10: Every Saturday night 11:00 pm reboot SMB command: 0 * * 6/ETC/INIT.D/SMB restart

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.