Little Knowledge Point--crontab

Source: Internet
Author: User

Objective

This two weeks learned a lot of things, but also the Golang language basic grasp, the harvest is quite a lot. In the process of doing security, there are many tasks that need to be performed regularly, such as the number of crawl hosts, port data, and so on, which can not escape the Linux crontab command, today to share a little bit of crontab this small knowledge point. Remind everyone, in the large company server, the deployment is basically CentOS, so you want to learn the operation of CentOS, we recommend you look at "Bird's-brother private cuisine"

Command description

The crontab command executes specified system instructions or shell script scripts at a fixed interval of time. The units of the time interval can be any combination of minutes, hours, days, months, weeks, and more.

Command format

crontab [-u user] File

Or

crontab [-u user] [-e |-l |-r]

Parameter interpretation

    • -u User: Used to set a user's crontab service, there may be many users under Linux, each user corresponds to a crontab task list.
    • File: Is the name of the command file, which indicates that file is loaded as a crontab task list.
    • -E: Edits the contents of a user's crontab file, or represents the current user if the user is not specified.
    • -L: Displays the contents of a user's crontab file, or represents the current user if the user is not specified.
    • -r: Deletes a user's crontab file from the/var/spool/cron directory and deletes the crontab file for the current user if the user is not specified.
    • -I: give confirmation prompt when deleting user crontab

crontab file

crontab command Parameters I believe you already understand, that crontab file, we need to follow what format to write content, cron service will help us to execute it regularly?

Basic format

Each of the tasks we want to perform is a line of content in the crontab file. The basic format for this line of content is as follows:

Minute hour day Month Week command

The Minute,hour,day,month,week,command is separated by a space and has the following formatting meanings:

    • Minute: Represents minutes, which can be any integer from 0 to 59.
    • Hour: Represents an hour, can be any integer from 0 to 23, and 0 represents 0 points.
    • Day: Represents a date, which can be any integer from 1 to 31, which is one of the days in one months.
    • 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.

Instance

After the above explanation, I wonder if you will use it? Here are some examples to help you understand and use.

    1. Execute commands 10th and 15 minutes per hour

10,15 * * * command

2.10th and 15 minutes of every two days from 3 o'clock in the morning to 5

10,15 3,5 */2 * command

3.4:45 ping Baidu every Saturday or Sunday

4 * * 6,0 ping www.baidu.com

4. Python-h every 30 minutes from 18:00 to 23:00 every Monday

0,30 18-23 * * 1 python-h

Crond Service

crontab files are performed by Crond Service guarantee, below are some small operations of Crond service.

Service Crond Start//Startup services

Service Crond stop//Shutdown services

Service Crond restart//Restart services

Service Crond Reload//Reload Configuration

Service Crond Status//view services statuses

Precautions

When defining multiple dispatch tasks in a crontab file, you need to pay particular attention to setting the environment variables, because when we perform a task manually, it is done in the current shell environment, the program can certainly find the environment variable, and the system automatically executes the task schedule, it will not load any environment variables, therefore, You need to specify all the environment variables required for the task to run in the crontab file so that the system performs the task scheduling without problems, so you have to ensure that all necessary paths and environment variables are provided in the shell script.

Note the following points:

1. The best way to write a global path when executing a script

2. Executed scripts, such as start.sh, if you reference an environment variable, you need to load the environment variable in the script using source. Some examples of start.sh include the following:

!/bin/shsource /etc/profile......

3. Manual execution of the script is fine, but the crontab is not executed, and this can also be an issue with environment variables. You can always introduce environment variables directly in the crontab command.

* * * * * source /etc/profile;python -h

At last

Follow the public number: Seven night security blog

    • Reply to "1": Collect Python Data Analysis Tutorial Package
    • Reply to "2": Collect Python Flask full set of tutorials
    • Reply to "3": Pick up a college machine learning tutorial
    • Reply to "4": Pick up Crawler tutorial
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.