Today did a database backup script, by the way the system has to learn Linux under the time to execute script settings. Linux in the timing of the implementation of the main use of the crontab file to add a custom program to execute, the setting is slightly more complex than Windows (because there is no graphical interface), but it is not very complex, basically used once can remember, the key is to remember/var/spool/ Cron this directory. Here's a look at the specific usage:
First look at the/etc/crontab file:
$ cat/etc/crontab
shell=/bin/bash
path=/sbin:/bin:/usr/sbin:/usr/bin
mailto=root
HOME=/
# Run-parts * * * * root
run-parts/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily
4 * * 0 root Run-parts/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly
The first four lines are the environment variables that set up cron tasks to run. The value of the shell variable specifies the shell environment used by the system (the sample is the Bash shell), and the path variable defines the paths that execute the command. The cron output 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.
The description format for each row of tasks in the file/etc/crontab is as follows:
Minute hour day Month DayOfWeek command
- minute-integers from 0 to 59
- hour-integers from 0 to 23
- Day-an integer from 1 to 31 (must be a valid date for the specified month)
- month-integers from 1 to 12 (or months, such as a short or Feb)
- DayOfWeek-integers from 0 to 7, 0 or 7 to describe Sunday (or by sun or mon shorthand)
- command-commands that need to be executed (can be used as Ls/proc >>/tmp/proc or commands to execute a custom script)
- Root indicates to run as root
- Run-parts means a folder followed by all the scripts under that folder.
For each of these statements, the asterisk (*) represents all the available values. For example, when referring to a month, the command is expressed on a monthly basis (subject to other restrictions).
The hyphen (-) between integers denotes an integer column, for example 1-4 means an integer 1,2,3,4
Specifies that values are separated by commas. For example: 3,4,6,8 represents these four specified integers.
The symbol "/" specifies the stepping setting. "/" represents a stepping value. If the 0-59/2 definition is executed every two minutes. The stepping value can also be indicated by an asterisk. such as */3 is used to run the specified task every three months.
The comment line, beginning with "#", is not executed.
If a cron task needs to be performed periodically rather than by hour, day, week, or month, you need to add the/ETC/CRON.D directory. All files and files in this directory have the same/etc/crontab syntax, look at the sample:
# The memory usage of the system every Monday # at 3:30AM in the
file/tmp/meminfo
3 * * Mon cat/proc/m Eminfo >>/tmp/meminfo
# Run custom scrīpt the ' I ' every month at 4:10am
4 1 * */root/scrīpts/b ackup.sh
Users other than the root user can perform crontab configuration scheduling tasks. All user-defined crontab are stored under the directory/var/spool/cron, and the tasks are executed as creators. To create a crontab with a specific user, log on as the user, execute the command crontab-e, and start the editing software editor crontab specified in Visual or editor. The file content is the same as the/etc/crontab format. Examples are as follows:
0 3 * * */home/dbbackup/db1backup.sh backup
0 4 * * */home/dbbackup/db2backup.sh Backup
Represents the execution of the/home/dbbackup/db1backup.sh backup,4 point execution/home/dbbackup/db2backup.sh backup at 3 o ' Day, if it is executed every five minutes:
*/5 * * * */home/dbbackup/db2backup.sh Backup
When the changed crontab needs to be saved, the file is saved in the file/var/spool/cron/username as follows. The file name will vary according to the user name.
The Cron service checks for changes under/etc/crontab,/etc/cron.d/,/var/spool/cron files every minute. If a change is found, it is downloaded to the memory. Therefore, even if the crontab file changes, the program does not need to reboot. Recommended custom tasks to use the CRONTAB-E command to add, after exiting with the/etc/init.d/crond Restart command to restart the Crond process, the official file said no restart process, but I encountered no reboot can not run the task. Beginning do not know what the run-parts in the/etc/crontab file means, directly to the/etc/crontab format plus always can't run, then know Run-parts refers to the following is the folder.
Here is an introduction:
************************************************************************************
Cron is a regular execution tool under Linux that can run jobs without human intervention. Because Cron is a built-in service for Linux, it does not automatically get up and you can start and close this service in the following ways:
/sbin/service crond Start//start service
/sbin/service Crond stop//off service/sbin/service crond restart
//restart service
/sbin/ Service Crond Reload//Reload Configuration
You can also start this service automatically when the system is started:
At the end of the/etc/rc.d/rc.local script, add:
/sbin/service Crond Start
Now that the Cron service is in the process, we can use this service, and the Cron service provides the following interfaces for everyone to use:
1. Edit directly with crontab command
The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:
Crontab-u//Set a user's Cron service, the general root user needs this parameter
crontab-l//list the details of a user's cron service
crontab-r//Remove the Cron service for no users
crontab-e//Edit a user's cron service
For example, root view your cron settings: Crontab-u root-l
Again for example, Root wants to delete Fred's cron settings: Crontab-u fred-r
When editing a cron service, there are some formatting and conventions for editing the content, input: Crontab-u root-e
into the vi editing mode, the contents of the edit must conform to the following format: */1 * * * * ls >>/tmp/ls.txt
The first part of this format is the set of time, the next part is the command to execute, if there are too many commands to execute, you can write these commands into a script, and then call the script here directly, and then remember to write the full path of the command. Time set we have a certain agreement, the front five * number represents five digits, the number of the value range and the meaning is as follows:
- Minutes (0-59)
- Hours (0-23)
- Date (1-31)
- Month (1-12)
- Week (0-6)//0 on behalf of Sunday
In addition to the numbers there are a few special symbols is "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" representing from a number to a number, "," separate several discrete numbers. Here are a few examples to illustrate the problem:
Every morning at 6.
0 6 * * * echo "Good morning." >>/tmp/test.txt//Note that pure echo does not see any output from the screen because cron emails any output to Root's mailbox.
Every two hours
0 */2 * * echo "Have a break now." >>/tmp/test.txt
Between 11 o'clock and 8 in the morning, every two hours, eight in the morning.
0 23-7/28 * * * echo "Have a Good Dream:)" >>/tmp/test.txt
4th a month and a week on Monday to three in the morning 11.
January 1 morning, 4.
After each edit of a user's cron settings, Cron automatically generates a file with the same name as this user under/var/spool/cron, and the user's cron information is recorded in this file, which is not directly editable and can only be edited with CRONTAB-E. After Cron starts, read the file every time it is written, and check if you want to execute the commands inside. Therefore, you do not need to restart the Cron service after this file has been modified.
2. Edit/etc/crontab File Configuration cron
The cron service does not only have to read all the files in the/var/spool/cron every minute, but also read the/etc/crontab, so we can configure the file to do something with the cron service. The crontab configuration is for a user, and editing/etc/crontab is a task for the system. The file format for this file is:
Shell=/bin/bash
path=/sbin:/bin:/usr/sbin:/usr/bin
mailto=root//If there is an error, or if there is data output, the data is sent to this account as a message
home=/ //user Run path, this is the root directory # run-parts * * * * * *
root run-parts/etc/cron.hourly//Hourly Execute/etc/cron.hourly script
4 * * * * root run-parts/etc/cron.daily//daily execution/etc/cron.daily script
4 * * 0 root run-parts/etc/cron.weekly//weekly execution Scripts in/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute script within/etc/cron.monthly
Attention to the "run-parts" This parameter, if you remove this parameter, then you can write a script to run the name, not the folder name.
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.