Crontab (scheduled task operation)

Source: Internet
Author: User

Scheduled tasks automatically perform task operations at a certain time point.
You can use crontab to perform scheduled operations during pgsql backup. Share
Specific scheduled editing command: crontab-e
First, analyze the usage policy from the crontab file. Under the root user, there is a file crontab under/etc. Its content is as follows:
[[Email protected] ~] # Cat/etc/crontab
Shell =/bin/bash
Path =/sbin:/bin:/usr/sbin:/usr/bin
Mailto = root
Home =/

# For details see man 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 lines in the file are crontab environment variables. The shell variable value specifies the shell environment used by the system (this example is bash shell ), the PATH variable defines the path for executing commands. Cron output is sent to the username defined by the mailto variable by email. If the mailto variable is defined as a null string (mailto = ""), the email will not be sent. When executing commands or scripts, the home variable can be used to set the base directory.
Note: The above system does not need to be modified by default!

Under the root user, you can directly add a scheduled task script to the vim/etc/crontab file, while other common users can add scripts through crontab-e.

After editing, you can use crontab-L to view the script information.

Script parsing of scheduled commands:
The script format is as follows:
Minute hour day month week user-name command
Minute --- minute (0-59)
Hour ------ hour (0-23)
Day -------- date (1-31)
Month --- month (1-12)
Week ---- Week (0-6) // 0 indicates Sunday
In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "*/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers. The following examples illustrate the problem:

Analyze the timing script with a specific example:
Pgslq. Sh is the script to be executed, and the content is the backup operation or other task script to be performed.
1 ***/home/Postgres/pgsql. Sh
Indicates that the script is executed in the first minute of every hour.
2 3 ***/home/Postgres/pgsql. Sh
Indicates that the script is executed at every day.
1 1 ** 0/home/Postgres/pgsql. Sh
Indicates that the script is executed at 01:01 every week.
1 1 1 **/home/Postgres/pgsql. Sh
Script Execution is performed at 01:01 every month.

The easy mistake is that the first minute of every hour is usually executed every minute. Pay attention to the difference between the two:
1 ***/home/Postgres/pgsql. Sh
Indicates that the script is executed in the first minute of every hour.
*/1 *****/home/Postgres/pgsql. Sh
Indicates that the script is executed every minute.
Therefore, remember the difference between the "/" symbol.
"-" Usage:
0 10 ** 1-3/home/Postgres/pgsql. Sh
Indicates that the script is executed at am from Monday to Wednesday.
0 10 ** 1, 3, 5/home/Postgres/pgsql. Sh
Indicates that the script is executed at am on Monday, Wednesday, and Friday every week.
These are almost common possibilities of scheduled tasks.
In the table, the user-name indicates the user where the script is located. Generally, it is impossible for the root user to write the script in the project, therefore, we may directly write scheduled scripts under normal users, directly execute crontab-E, and write scheduled tasks.
After you edit the cron settings of a user, cron automatically generates a file with the same name under/var/spool/cron. The Cron information of this user is recorded in this file, this file cannot be edited directly. You can use crontab-e to edit it. After starting Cron, read the file every minute and check whether the command in it is executed. Therefore, you do not need to restart the cron service after the file is modified.

Crontab (scheduled task operation)

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.