Linux timed Tasks crontab detailed

Source: Internet
Author: User

Original address:http://edu.codepub.com/2011/0104/28518.php 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 the/etc/crontab file: $ cat/etc/crontabShell=/bin/bashPath=/sbin:/bin:/usr/sbin:/usr/binMailto=roothome=/# 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.monthlyThe first four rows are the environment variables that set 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. each line of the task in the file/etc/crontab is described in the following format: 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 -an integer from 1 to 12 (or a month such as the Jan or Feb abbreviation) DayOfWeek -integers from 0 to 7, 0 or 7 to describe Sunday (or as represented by sun or mon shorthand) Command -Commands to execute (commands that can be used as Ls/proc >>/tmp/proc or execute custom scripts) root indicates to run as root userRun-parts means a folder followed by all the scripts under that folderfor each of these statements, the asterisk (*) represents all available values. For example, when referring to month, the command is executed monthly (subject to other restrictions). the hyphen (-) between integers denotes an integer column, for example 1-4 means an integer 1,2,3,4The specified value is separated by commas. Such as: 3,4,6,8 represents these four specified integers. the symbol "/" specifies the stepping setting. "/" 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 running. 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 the/ETC/CRON.D directory. All files and files in this directory are/etc/crontab in the same syntax, see the sample:# Record the memory usage of the system every Monday# at 3:30AM in the File/tmp/meminfo3 * Mon cat/proc/meminfo >>/tmp/meminfo# Run Custom scrīpt the first day of every month at 4:10AM4 1 * */root/scrīpts/backup.shusers other than the root user can perform crontab configuration scheduled tasks. All user-defined crontab are stored under directory/var/spool/cron, and 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. The file content is the same as the/etc/crontab format. Examples are as follows:0 3 * * */home/dbbackup/db1backup.sh backup0 4 * * */home/dbbackup/db2backup.sh backuprepresents 3 points per day to perform/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 backupwhen the changed crontab needs to be saved, the file is saved in the file/var/spool/cron/username as follows. The file name differs depending on the user name. The Cron service checks for changes in/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 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/etc/crontab file run-parts what meaning, direct command in accordance with the/etc/crontab format plus always can not run, later only know run-parts refers to the folder followed by. 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//Startup service/sbin/service Crond stop//Shut down service/sbin/service crond Restart//Restart service/sbin/service Crond Reload//Reload configuration You can also start the service automatically when the system starts:at the end of the/etc/rc.d/rc.local script, add:/sbin/service crond Startnow cron This service is already in the process, we can use this service, Cron service provides the following kinds of interfaces for everyone to use:1. Edit directly with crontab commandThe 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, which is usually required by the root user when executing this commandcrontab-l//list details of a user 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-lagain, for example, Root wants to delete Fred's cron settings: Crontab-u fred-rwhen editing the cron service, the edited content has some formatting and conventions, input: Crontab-u root-e Enter VI edit mode, the content of the edits must conform to the following format: */1 * * * * ls >>/tmp/ls.txtThe first part of this format is the time setting, 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. Time setting we have a certain agreement, the preceding five * number represents five numbers, the value range and meaning of the numbers are as follows:minutes (0-59)hours (0-23)Date (1-31)month (1-12)Week (0-6)//0 representative SundayIn 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:every morning at 6.-----------------0 6 * * echo "Good morning." >>/tmp/test.txt//Note simply Echo, no output is visible from the screen, because cron emails any output to root. every two hours-----------------0 */2 * * echo "has a break now." >>/tmp/test.txtevery two hours between 11 o'clock and 8 in the morning, eight in the morning.-----------------0 23-7/28 * * * echo "a good Dream:)" >>/tmp/test.txtevery month, number 4th and Monday to Sunday, three a.m., 11 .-----------------0 4 * 1-3 command LineJanuary 1 morning, 4.-----------------0 4 1 1 * Command Lineafter 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 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 the/etc/crontab file configuration cron Cron Service every minute not only to read all the files within/var/spool/cron, but also to read a/etc/crontab, so we configure this file can also use the Cron service to do something. The crontab configuration is for a user, while the edit/etc/crontab is a task for the system. 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//hourly execution of scripts within/etc/cron.hourly4 * * * Root run-parts/etc/cron.daily//daily execution of scripts within/etc/cron.daily4 * * 0 root run-parts/etc/cron.weekly//weekly execution of scripts within/etc/cron.weekly4 1 * * Root run-parts/etc/cron.monthly//monthly to execute scripts within/etc/cron.monthlyattention to the "run-parts" This parameter, if you remove this parameter, you can later write to run a script name, not the folder name. ************************************************************************************

Linux timed Tasks crontab detailed

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.