Explore Linux-CRONTAB to schedule tasks

Source: Internet
Author: User
Explore Linux & mdash; CRONTAB scheduled task cron is a scheduled execution tool in linux that can run jobs without manual intervention. Since Crond is a built-in service in Linux, it is automatically started with the system by default. of course, you can manage and maintain con... explore Linux-CRONTAB scheduled task cron is a linux scheduled execution tool that can run jobs without manual intervention. Since Crond is a built-in service in Linux, it is automatically started with the system by default. you can also manage and maintain the contab scheduled task service as follows. /Sbin/service crond start // start the service/sbin/service crond stop // close the service/sbin/service crond restart // restart the service/sbin/service crond reload // reload in the following chapter, we will talk about how to effectively use the crontab service to manage scheduled tasks:
1. directly use the crontab command to edit the cron service to provide the crontab command to set the cron service. The following are some parameters and instructions for this command: crontab-u // Set the cron service of a user, generally, only the root user can set the scheduled task crontab-l for other users. // list the details of the current user's cron service. www.2cto.com crontab-r // delete the current user's cron service crontab-e // edit The cron service of the current user, for example, root, to view your cron settings: crontab-u root-l again, for example, root wants to delete the cron setting of fred: crontab-u fred-r when editing the cron service, the edited content has some formats and conventions. input: crontab-u root-e enters the vi editing mode. the edited content must conform to the following format: */1 ***** ls>/tmp/ls.txt: crond resident command scheduled by the task
2. linux task scheduling is divided into the following two types: * System execution: the work to be executed by the system periodically, such as backing up system data and clearing cache * personal execution: the work that a user does on a regular basis, such as checking whether there are new emails on the email server every 10 minutes, can be set by each user.
3. cron file syntax: hour, day, month, week, command 0-59 0-23 1-31 1-12 0-6 command note: value range, 0 indicates that a common line on Sunday corresponds to a task.
4. remember the meanings of several special symbols: "*" indicates a number in the value range, "/" indicates "every", "-" indicates a number to a number ,", "Separate several discrete numbers 5. you can use the crontab-e command to edit the syntax of the task scheduling setting file. The cron file of the corresponding user in/var/spool/cron is edited, you can also directly modify the format of the/etc/crontab file as follows: Minute Hour DayMonth Dayofweek command Minute Hour day month day Weekly command each field represents the meaning of the following: minute executes this task in the Minute of each Hour. Hour executes this task in the Hour of every Day. Day of every Month. run this task in the Month. Day of every year. run this task in the Day of every week. note: in these fields, except that "Command" is a field that must be specified each time, other fields are optional fields. Decide. For unspecified fields, use "*" to fill their positions. Example of www.2cto.com: * ls ----- specify to execute the ls command once every 5th minutes of an hour. * ls ----- specify to execute the ls command 30 7 8 * ls at every day. execute the ls command 30 5 8 6 * ls at on October 8. execute the ls command 30 6*0 ls at on October 30. specify to execute the ls command at every Sunday. note: 0 indicates Sunday, 1 indicates Monday, and so on. sun indicates Sunday, mon indicates Monday, and so on. 30, 20 ** ls: execute the ls command at on the 10th and 20th of every month [note: ", [note: "-" is used to connect to consecutive periods] */15 ***** ls executes the ls command every 15 minutes [that is, the ls command is executed every hour for 0th 15 30 45 60 minutes] 30 6 */10 ** ls every month, run the ls command at every 10 days [that is, run the ls command at on the 1st, 11th, 21st, and 31st of every month.] Run/etc/cron at every day as root. all executable files in the daily directory are 50 7 * root run-parts/etc/cron. daily [Note: run-parts parameter indicates that all executable files in the following directory are executed.]
7. There are two ways to add a new scheduling task: 1. enter crontab-e in the command line and add the corresponding task. The wq disk is saved and exited. 2. directly edit the/etc/crontab file, that is, vi/etc/crontab, and add the corresponding task.
8. view the scheduling task crontab-l // list all the current scheduling tasks crontab-l-u jp // list all scheduling tasks of the user jp 9. delete the task scheduling task crontab-r // delete all task scheduling jobs. 10. Redirection example 1 of task scheduling execution results: run the ls command at every day and output the result to the/jp/test file for 30 5 * ls>/jp/test 2> & 1 Note: 2> & 1 indicates the execution result and error message. Edit the/etc/crontab file and configure the cron service to read not only all files in/var/spool/cron once per minute, but also/etc/crontab once, therefore, we can use the cron service to configure this file. Crontab configuration is intended for a user, and editing/etc/crontab is a system task. The file format of this file is: SHELL =/bin/bash PATH =/sbin:/bin:/usr/sbin:/usr/bin MAILTO = root // if an error occurs, or there is data output. The data is sent to the account HOME as an email = // path for the user to run, here is the root directory # run-parts www.2cto.com 01 ***** root run-parts/etc/cron. hourly // Run/etc/cron hourly. hourly script 02 4 *** root run-parts/etc/cron. daily // Run/etc/cron every day. daily script 22 4 ** 0 root run-parts/etc/cron. weekly // Run/etc/cron every week. weekly script 42 4 1 ** root run-parts/etc/cron. m Onthly // Run/etc/cron every month. note the "run-parts" parameter for the script in monthly. If this parameter is removed, you can write a script name to be run later, instead of the folder name. for example: 1. enter crontab-e in the command line and add the corresponding task. The wq disk will exit. 2. directly edit the/etc/crontab file, that is, vi/etc/crontab, and add the corresponding task 11 2 21 10 * rm-rf/mnt/fb author Wu Weilong.
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.