Linux timed Task Crontab detailed _ scheduled backup

Source: Internet
Author: User
Tags crontab syntax

Article Source:http://blog.chinaunix.net/uid-7552018-id-182133.html

Today I made a backup script of the database, and by the way the system had to learn the settings of the timed execution script under Linux. Linux scheduled execution is mainly used in the crontab file to add a custom plan to execute, the setup is slightly more complicated than Windows (because there is no graphical interface), but it is not very complex, basically used once to remember, the key is to remember/var/spool/Cron this directory. Here's a look at the specific usage: first look at/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.hourly4 * * * Root run-parts/etc/cron.daily4 * * 0 root run-parts/etc/cron.weekly4 1 * * Root run-parts/etc/cron.monthly The first four lines are the environment variables that set up the cron task to run. The value of the shell variable specifies the shell environment used by the system (the example is 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. File/etc/each row of tasks in Crontab is described in the following format: Minute hour day month DayOfWeek command minute-integer hour from 0 to 59-integer 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 Jan or Feb abbreviated) DayOfWeek-integers from 0 to 7, 0 or 7 to describe Sunday (or to be represented by sun or Mon shorthand) command-Commands to execute (available as Ls/proc >>/tmp/proc or commands to execute a custom script) root means running run as root-parts is followed by a folder, to perform all scripts under that folder for each of the above statements, asterisks (*) to indicate all available values. For examplethe command that represents monthly execution (subject to other restrictions) when referring to month. The hyphen between integers (-) represents an integer column, such as 1-4 meaning integer 1,2,3,4The specified value is separated by commas. such as:3,4,6, 8 represents the four specified integers. Symbol "/"Specify the stepping settings. "/" indicates a stepping value. such as the 0-59/2 definition is executed every two minutes. The step value can also be represented by an asterisk. such as */3 is used to run a specified task every three months. A comment line that begins with "#" is not executed. If a cron task needs to be performed on a regular basis instead of by the hour, day, week, and month, you need to add/ETC/CRON.D directory. All files and files in this directory/etc/crontab syntax is the same, see example: # Record the memory usage of the system every Monday # at3:30AM in the file/tmp/Meminfo3 * Mon cat/proc/meminfo >>/tmp/Meminfo # Run custom scrīpt the first day of every month at4: 10AM4 1 * */root/scrīpts/backup.sh can perform crontab configuration scheduling tasks in addition to root users. All user-defined crontab stored in the directory/var/spool/cron, the task is executed as the creator. To create a crontab with a specific user, first log in as that user, execute command crontab-e, and the system initiates the editing crontab that is specified in visual or editor. Document content and/etc/The crontab format is the same. Examples are as follows:0 3 * * */home/dbbackup/db1backup.sh Backup0 4 * * */home/dbbackup/db2backup.sh backup means 3 o ' Day execution/home/dbbackup/db1backup.sh backup,4 Point Execution/home/dbbackup/db2backup.sh Backup, if it is executed every five minutes, can be changed to:*/5 * * * */home/dbbackup/db2backup.sh Backup When the changed crontab needs to be saved, the file is saved in the file as follows/var/spool/cron/username. The file name differs depending on the user name. Cron service is checked every minuteChanges under/etc/crontab,/etc/cron.d/,/var/spool/cron files. If a change is found, it is downloaded to the memory. Therefore, even if the crontab file changes, the program does not need to be restarted. The recommended custom task is added using the CRONTAB-E command, which restarts the crond process with the/etc/init.d/crond Restart command, and the official file says that it does not restart the process, but I am not able to run the task without restarting. Start do not know what the/etc/crontab file in the run-parts meaning, direct command in accordance with the/etc/crontab format plus always can not run, later know run-parts refers to a folder followed by a. The following is attached to an introduction:************************************************************************************Cron is a timed execution tool under Linux that can run a job without human intervention. Since Cron is a built-in service for Linux, it does not automatically get up, and you can start and shut down this service in the following ways:/sbin/service Crond Start//Start the service/sbin/service Crond Stop//Close Service/sbin/service Crond Restart//Restart Service/sbin/service Crond Reload//Reload ConfigurationYou can also start the service automatically when the system starts:/etc/rc.d/rc.local The end of this script plus:/sbin/Service Crond Start now Cron is already in the process, we can use this service, Cron service provides the following kinds of interfaces for everyone to use:1. Use the crontab command to edit the cron service to provide the crontab command to set the Cron service, here are some parameters and instructions for this command: crontab-U//set a user's Cron service, which is typically required by the root user when executing this commandCrontab-l//list details of a user's cron serviceCrontab-r//Delete a cron service with no usersCrontab-e//edit a user's cron serviceFor example, root to 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 the cron service, the edited content has some formatting and conventions, input: Crontab-U Root-e into VI editing mode, the content of the edit must conform to the following format:*/1 * * * * ls >>/tmp/Ls.txt The first part of this format is the setting of the time, the next part is the command to execute, if you want to execute too many commands, you can write these commands into a script, and then call this script directly here, you can recall the full path of the command when the call. The setting of time we have a certain agreement, the front of five*Number represents five digits, and the value range and meaning of the number are as follows: minutes (0-59) hours (0-23) Date (1-31) Month (1-12) Week (0-6)//0 Delegates SundayBesides the numbers, there are a few special symbols."*", "/" and "-", ",", * represents all the numbers within the range of values, "/" stands for each meaning, "*/5" means every 5 units, "-" represents 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 echo alone does not show any output from the screen, because cron emails any output to the root mailbox. every two hours-----------------0 */2 * * * echo "has a break now." >>/tmp/test.txt 11 o'clock to 8 a.m., every two hours, eight a.m. .-----------------0 23-7/28 * * * echo "Have a good dream:)" >>/tmp/Test.txt, 4th per month and Monday to Sunday, three a.m. 11 .-----------------0 11 4 * 1-3command line, 4 a.m., January 1.-----------------0 4 1 1 *command line each time you finish editing a user's cron settings, Cron automatically/var/spool/cron generate a file with the same name as this user, this user's cron information is recorded in this file, this file can not be directly edited, can only be used crontab-e to edit. After Cron starts, read the file once every one of the clocks, 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. 2. Edit/etc/crontab file configuration cron cron Service not only to read once per minuteAll the files within the/var/spool/cron also need to be read once/etc/crontab, so we can configure this file to do something with the cron service. The crontab configuration is for a user, while the edit/etc/Crontab is a system-specific task. The file format for this file is: SHELL=/bin/Bashpath=/sbin:/bin:/usr/sbin:/usr/Binmailto=root//If an error occurs, or if there is data output, the data is sent to this account as an emailhome=///user-run path, here is the root directory# run-Parts* * * * * Root run-parts/etc/cron.hourly//execute scripts within/etc/cron.hourly per hour4 * * * Root run-parts/etc/cron.daily//Execute scripts within/etc/cron.daily daily4 * * 0 root run-parts/etc/cron.weekly//Execute scripts within/etc/cron.weekly every week4 1 * * Root run-parts/etc/cron.monthly//every month to execute scripts within the/etc/cron.monthlyattention, everyone ."Run-parts"This parameter, if you remove this parameter, you can write a script name to run, instead of the folder name. ************************************************************************************

Linux timed Task Crontab detailed _ scheduled backup

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.