Crontab (timed task operation)

Source: Internet
Author: User
A timed task is the automatic task operation at a certain point in time.
It is convenient to use the crontab to make the Pgsql backup using the timed operation. So share
Specific timed edit command: crontab-e
First, from the crontab file analysis using the policy, the root user, under/etc has a file crontab, the content is as follows
[Root@myzk ~]# Cat/etc/crontab
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/

# for details see Mans 4 Crontabs

# Example of Job definition:
#.----------------Minute (0-59)
# |.-------------Hour (0-23)
# | |.----------DAY of Month (1-31)
# | | |.-------month (1-12) OR jan,feb,mar,apr ...
# | | | |.----DAY of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * * user-name command to be executed
The first four rows of the file are crontab environment variables, and the value of the shell variable specifies the shell environment used by the system (the example is the Bash shell), which defines the path to execute the command. The output of Cron is sent as an e-mail message to the user name defined by the mailto variable. If the MAILTO variable is defined as an empty string (mailto= ""), the e-mail message is not sent. The home variable can be used to set the base directory when executing a command or script.
Note: The system above will not be modified by default.

Under the root user, you can add a timed task script directly to the Vim/etc/crontab file, and the script can be added by Crontab–e under other ordinary users.

Editing complete, you can view the script information with Crontab–l

Timed Command Script parsing:
You can see from the table that the script is in the following format:
Minute hour day month Week user-name command
Minute---minutes (0-59)
hour------hours (0-23)
Day--------Date (1-31)
Month---months (1-12)
Week----Week (0-6)//0 on behalf of Sunday
In addition to the numbers there are several special symbols are "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" represents from a number to a number, "," separate several discrete numbers. Here are a few examples to illustrate the problem:

Analyze timed scripts with specific examples:
Pgslq.sh is the script that needs to be executed, the content is the backup operation or other task script
1 * * * */home/postgres/pgsql.sh
Represents the first minute of every hour to execute the script
2 3 * * */home/postgres/pgsql.sh
Represents 3 points per day 2 minutes to execute the script
1 1 * * 0/home/postgres/pgsql.sh
Represents a weekly 1:1 execution of the script
1 1 1 * */home/postgres/pgsql.sh
Represents the execution of a script at 1:1 every month

It's easy to make a mistake that usually takes the first minute of every hour to do it every minute, which is the difference between the two:
1 * * * */home/postgres/pgsql.sh
Represents the first minute of every hour to execute the script
*/1 * * * */home/postgres/pgsql.sh
Represents the execution of the script every minute
So here's to remember the difference between "/" this symbol
Usage of "-":
0 * * 1-3/home/postgres/pgsql.sh
That is, every Monday to Wednesday morning, 10. Execute the Script
0 10 * * 1, 3, 5/home/postgres/pgsql.sh
Represents the weekly Monday, Wednesday, and Friday of the morning 10 to execute the script.
These are roughly the most common possible timing scripts for timed tasks.
In the table see User-name this represents the user that the script is in, generally in the project can not be a root user to write, so we may directly under the ordinary user to write timed script, directly execute crontab–e, write timed tasks.
After editing a user's cron settings, Cron automatically generates a file with the same name as the user under/var/spool/cron, and the cron information for this user is recorded in this file, which cannot be edited directly and can only be edited with CRONTAB-E. After Cron starts, read the file once every minute and check to see if you want to execute the command inside. Therefore, the Cron service does not need to be restarted after this file has been modified.

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.