Linux timed Task--crontab

Source: Internet
Author: User

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 start 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, 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-eEnter 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 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: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.txt every 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 Line 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 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 cronCron 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/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root//If an error occurs, or if there is data output, the data is sent to this account as an email
home=/ //user-run path, here is the root directory
# Run-parts
* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * root run-parts/etc/cron.daily //daily execution of scripts within/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly//weekly execution of scripts within/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute scripts within/etc/cron.monthly attention 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 Task--crontab

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.