Linux timed Task cron detailed

Source: Internet
Author: User

Abstract: I believe that many Linux enthusiasts or the development process in the use of Linux environment it. One of the crontab is a very powerful timing task executor. For example, we can set a script for when to execute a task, and the system will start the task within a specified time. The results are output to the specified file or notification to the administrator when we need it. Can save us a lot of time. Here's a look at the specific usage of crontab.

The crontab command is common in Linux operating systems and is used to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. The word derives from the Greek language Chronos (χρνο), which was originally meant to be time. Typically, crontab stored instructions are activated by the daemon, Crond often run in the background, checking every minute for scheduled jobs to be executed. This type of work is generally called cron jobs.

The crontab file contains a series of jobs and instructions sent to the cron daemon. Each user can have their own crontab file, while the operating system holds a crontab file for the entire system, which is usually stored in sub-directories such as/etc or/etc, and this file can only be modified by the administrator, which is generally the root user.

Each line of the crontab file follows a specific format, separated by a space or tab into several realms, each of which can place a single or multiple numeric values.

Use Syntax:

crontab [-E [Username]|-l [Username]|-r [username]|-v] [username]| File]

Crontab is used to allow a user to execute a program at a fixed time or at a fixed interval, in other words, a user-like schedule. -u user refers to the setting of the time table for the specified user, which presupposes that you must have permission (for example, root) to specify another's time schedule. If you do not use the-u user, it means setting your own schedule. Parameters:-E [UserName]: Perform a text editor to set the time table, the default text editor is VI, if you want to use a different text editor, you first set the VISUAL environment variables to specify the use of that text editor (for example, Setenv VISUAL Joe)-R [ UserName]: Delete the current schedule table-l [UserName]: Lists the current schedule table-V [UserName]: Lists the status of the user cron job in the format of the following table: F1 F2 F3 f4 F5 program where F1 is expressed in minutes, F2 represents hours, F3 Represents the day ordinal of a month, F4 represents the month, and F5 represents the day of the one week. Program represents the programs to execute. When F1 is * means every minute to execute PROGRAM,F2 as * means to execute the program every hour, the rest of the analogy when the F1 is a-B to be executed from the time of the first to the minute, the F2 is a-B to be executed from the first to the other, and the other analogy When F1 is */n, it is executed every n minutes, F2 is */n for every n-hour interval, and the rest of the analogy when F1 is a, B, C,... A, B, C,... Minutes to execute, F2 for a, B, C,... The time indicated that the first, B, c ... Hours to execute, the rest of the analogy users can also put all the settings in the file file, the Crontab file method to set the time table. Because the unix  version is different, there are some differences in syntax, such as setting interval execution in HP UNIX AIX If syntax error occurs in */n mode, where interval execution can only be enumerated in this type of UNIX. Use VI to edit a file cronfile, and then enter a well-formed schedule in this file. When the edits are complete, save and exit. [2]  Enter $: crontab cronfile on the command line to submit the Cronfile file to the C R o N process, while a copy of the newly created cronfile has been placed in/V a r/s P o l/C r o n directory, the file name is the user name. Example: every 0 minutes per day of every hour of the month/bin/ls:0 * * * * */bin/ls in December, every day in the morning 6 to 12, every 20 minutes to perform/usr/bin/backup:*/20 6-12 * 12 */ Usr/bin/backup Monday to Friday every 5:00 send a letter to alex_mail_name:0 * * 1-5 mail-s "HI" Alex_mail_name </tmp/maildata every day 0:20 midnight, 2:20, 4:20 .... Perform echo "haha" 0-23/2 * * * echo "haha" night 11 o'clock to 8 a.m. every two hours, 8 a.m. 0 23-7/2,8 * * * date in Hpunix, every 20 minutes, expressed as: 0,20,40 * * * * Instead of using the */n method, there is a syntax error NOTE: 1. When the program executes at the time you specify, the system will send you a letter showing what the program is doing, and if you do not wish to receive such a letter, please add >/dev/null 2>&1 after each line. 2.% in Crontab is considered to be newline, to use \ to escape. For example, if you have "date +%y%m%d" in crontab execution line, you must replace it with the following: "Date +\%y\%m\%d" creation crontab before considering submitting a crontab file to the cron process, The first thing to do is to set the environment variable editor. The cron process depends on it to determine which editor to use to edit the crontab file. 99 of UNIX and Linux users use VI, and if you do, then you edit the. profile file in the $home directory, adding a line like this: Editor=vi; Export editor and then save and exit. You may want to create a file named <user>cron, where <user> is the user name, in order to submit the crontab file you just created, you can put this newly created file as the parameter of the cron command: $ crontab Davecron now the file has been submitted to the Cron process, and a copy of the newly created file has been placed in the/var/spool/cron directory, and the filename is the user name (that is, Dave). List CrontAB file in order to list the crontab file, you can use: $crontab-L Edit crontab file if you want to add, delete, or edit the entries in the Crontab file, and the editor environment variable is set to VI, you can use VI to edit the crontab file. The corresponding command is: $ crontab-e can modify the crontab file and exit as if you were editing any other file using VI. Delete the crontab file in order to delete the crontab file, you can use: $ crontab-r Comment crontab file if you do not want to delete the written crontab file, add # before the crontab file to comment out the file. Recover lost crontab files if you accidentally delete the crontab file, assuming you have a backup in your $home directory, you can copy it to the/var/spool/cron/<username&gt, where <username > is the user name. If the copy cannot be completed due to a permissions issue, you can use: $ crontab <filename> where,<filename> is the file name of your copy in the $home directory. The output configuration in Crontab crontab is often configured to run script output as: >/dev/null 2>&1 to avoid content output in crontab run. The result of the shell command can be defined by ' > ' as the output/dev/null represents the empty device file > represents the redirect to where, for example: echo "123" >/HOME/123.TXT1 represents stdout standard output, the system default value is 1, so ">/dev/null" is equivalent to "1>/dev/null" 2 means stderr standard error & means equivalent, 2>&1, indicating that 2 of the output redirect equals 1 then the meaning of the redirected output statement: 1>/dev/ Null first indicates that the standard output is redirected to an empty device file, which means no information is output to the terminal and no information is displayed. 2&GT;&AMP;1 indicates that standard error output redirection is equivalent to standard output because the standard error output is redirected to an empty device file because the standard output was previously redirected to an empty device file.   Welcome to Add.

Linux timed Task cron 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.