How to use cron for scheduled task execution in linux

Source: Internet
Author: User
Tags cron script
In linux, the name of the method for executing tasks with cron: crontab permission: all users use crontabfile [-uuser]-to replace the current crontab with the specified file. Crontab-[-uuser]-replace the current crontab with the standard input. crontab-1 [u... in linux, the name of the method for executing tasks with cron: crontab permission: all users use crontab file [-u user]-to replace the current crontab with the specified file. Crontab-[-u user]-replace the current crontab with the standard input. crontab-1 [user]-list the current crontab of the user. crontab-e [user]-edit the current crontab of the user. crontab-d [user]-delete the current crontab of the user. crontab-c dir-specifies the crontab directory. Crontab file format: m h d m d cmd. basic format: *** command minute hour day month week command M: minute (0-59 ). * Or */1 indicates H: Hour (0-23) every minute ). (0 indicates 0.) D: Day (1-31 ). M: month (1-12 ). D: days in a week (0 ~ 6, 0 is Sunday ). The program to run in cmd is sent to sh for execution. the shell only contains three environment variables: USER, HOME, and SHELL: crontab is used to allow users to execute programs at a fixed time or interval. In other words, it is similar to the user's time table. -U user is used to set the time table of the specified user. The premise is that you must have the permission (for example, root) to specify the time table of another user. If-u user is not used, the time table is set. Parameter: crontab-e: run the text editor to set the time table. the preset text editor is VI. if you want to use another text editor, first, set the VISUAL environment variable to specify the text editor (for example, setenv VISUAL joe) crontab-r: delete the current time table crontab-l: list the current time table crontab file [-u user]-replace the current crontab with the specified file. The time table format is as follows: f1 f2 f3 f4 f5 program where f1 represents minutes, f2 represents hours, f3 represents the day of a month, and f4 represents the month, f5 indicates the day of the week. Program indicates the program to be executed. When f1 is *, the program is executed every minute. if f2 is *, the program is executed every hour, and so on. when f1 is a-B, it indicates that it will be executed from minute a to minute B, when f2 is a-B, it indicates that execution is performed from Hour a to hour B, and so on. when f1 is */n, it indicates that execution is performed every n minutes, if f2 is */n, the task is executed every n hours. Similarly, when f1 is a, B, c ,... a, B, c ,... execute in minutes. f2 is a, B, c ,... a, B, c... you can save all settings in the file and use crontab file to set the time table. Example: # run/bin/ls at every morning: 0 7 ***/bin/ls at PM every morning within March 31, December, run once every 3 hours/usr/bin/backup: 0 6-12/3*12 */usr/bin/backup from Monday to Friday send a letter to alex@domain.name at every day: 0 17 ** 1-5 mail-s "hi" alex@domain.name </tmp/maildata every day at midnight 00:20, 02:20, 04:20 .... run echo "haha" 20 0-23/2 *** echo "haha". Note: When the program is executed at the specified time, the system will send you a letter, display the content of the program execution. if you do not want to receive such a message, add>/dev/null 2> & 1 after each line is empty. Example 2: # every morning 06:10 10 6 * date # every two hours 0 */2 * date # every two hours between PM and am, am 0 23-7/2, 8 * ** date #4 of each month and 11: 0 11 a.m. from Monday to wed date of every week # a.m. of February 4: 0 4 1 jan * date example $ crontab-l lists the current crontab of a user. /usr/lib/cron. allow indicates who can use the crontab command. If it is an empty file, it indicates that no user can schedule a job. If this file does not exist and another file/usr/lib/cron. deny exists, the crontab command can be used only by users not included in this file. If it is an empty file, it indicates that any user can arrange jobs. If both files exist at the same time, cron. allow takes priority. if neither of them exists, only the super user can schedule a job. Some examples of crontab files: 30 21 ****/usr/local/etc/rc. d/lighttpd restart the above example indicates restarting apache at every night. 45 4, 22 **/usr/local/etc/rc. d/lighttpd restart the preceding example indicates restart apache on 4: 45 every month on Day 1, 10, and 22. 10 1 ** 6, 0/usr/local/etc/rc. d/lighttpd restart the example above indicates that apache is restarted at every Saturday and Sunday. The preceding example of-23 ***/usr/local/etc/rc. d/lighttpd restart indicates that apache is restarted every 30 minutes from to every day. 0 23 ** 6/usr/local/etc/rc. d/lighttpd restart the preceding example indicates that apache is restarted at every Saturday. **/1 ***/usr/local/etc/rc. d/lighttpd restart apache * 23-7/1 */usr/local/etc/rc every hour. d/lighttpd restart: restart apache 0 11 4 * mon-wed/usr/local/etc/rc every hour from. d/lighttpd restart: restart apache 0 4 1 jan */usr/local/etc/rc from every Monday to Wednesday. d/lighttpd restart apache at on January 1, January 1: Use commas to separate the time values. In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each. "*/5" indicates every five units. "-" indicates that a number is transferred to a number. "," separate several discrete numbers. Every morning at, **** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox. 0 */2 *** echo "Have a break now every two hours. ">/tmp/test.txt every two hours from PM to AM, 8 *** echo "Have a good dream :)">/tmp/test.txt AM, AM, Am- 3 command line, January 1, January 1 4 1 1 * after each command line editing a user's cron settings, cron automatically generates a file with the same name as/var/spool/cron. The cron information of this user is recorded in this file and cannot be edited directly, you can only use crontab-e for editing. The cron reads the file every minute after it is started, and checks whether to execute the commands in it. Therefore, you do not need to restart the cron service after the file is modified. 2. 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 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. monthly // monthly receipt Script 2 in/etc/cron. monthly. cron is a linux scheduled execution tool that can run jobs without manual intervention. Since Cron is a built-in service in Linux, but it does not automatically get up, you can use the following methods to start and close this service: /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 you can also configure to automatically start the service when the system starts: in/etc/rc. d/rc. add/sbin/service crond start at the end of the local script. now the Cron service is already in the process, and we can use this service, the Cron service provides the following interfaces for you to use: 1. directly use the crontab command to edit the cron service. The crontab command is provided to set the cron service. The following are some parameters and descriptions of this command: crontab-u // set a user's cron service, Generally, when executing this command, the root user needs this parameter crontab-l // to list the details of a user's cron service. crontab-r // deletes a user's cron service crontab-e/ /edit a user's cron service, such as root, to view their 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> the first part of the format of/tmp/ls.txt is the time setting, and the other part is the command to be executed. if too many commands are to be executed, you can write these commands into a script, and then directly call the script here. remember to write the complete path of the command during the call. We have a certain agreement on the time setting. the first five * numbers represent five numbers. the value range and meaning of the numbers are as follows: minute (0-59) is small? R (0-23) Date (1-31) month (1-12) Week (0-6) // 0 indicates Sunday. in addition to numbers, there are also several special symbols: "*", "/", "-", and ". * indicates the number in all values, "/" indicates the meaning of each. "*/5" indicates every five units. "-" indicates a number to a number ,", "It is easy to separate several discrete numbers, cron. first, a quick one: Step 1: Write a cron script file. For example, name a crontest. cron text file, only need to write a line: 15, 30, 45, 59 *** echo "xgmtest .......... "> xgmtest.txt indicates that the command is printed every 15 minutes. Step 2: Add a scheduled task. Run "crontab crontest. cron ". Step 3: If you are not at ease, enter "crontab-l" to check whether the scheduled task details are available: crontab usage the crontab command is used to install, delete, or list tables used to drive cron background processes. That is to say, the user puts the command sequence to be executed into the crontab file for execution. Each user can have their own crontab file. The following describes how to create a crontab file. Crontab files under/var/spool/cron cannot be directly created or modified. The crontab file is obtained through the crontab command. Assume that you have a username of foxy, and you need to create your own crontab file. First, you can use any text editor to create a new file, and then write the commands to be run and the time to be periodically executed to it. Then save the disk and exit. Assume the file is/tmp/test. cron. Then, use the crontab command to install the file and make it the user's crontab file. Type: crontab test. cron, and a crontab file is created. You can go to the/var/spool/cron directory and check that there is an additional foxy file. This file is the required crontab file. You can use the more command to view the FILE content and find that there are three lines of information in the FILE header: # do not edit this file-edit the master and reinstall. # (test. cron installed on Mon Feb 22 14:20:20 1999) # (cron version -- $ Id: crontab. c, v 2.13 1994/01/17 03:20:37 vivie Exp $) probably means: # Do not edit this file-if you need to change it, edit the source file and reinstall it. # Test. cron file installation time: 14: 20: 20 02/22/1999 if you want to change the command content, you still need to re-edit the original file and then use the crontab command to install it. Users who can use the crontab command are limited. If/etc/cron. if the allow file exists, only the listed users can use this command. if the file does not exist but cron. if the deny file exists, only users not listed in the file can use the crontab command. if neither file exists, it depends on the setting of some parameters, it is possible that only the super user is allowed to use this command, or that all users can use this command. The syntax format of the crontab command is as follows: crontab [-u user] file crontab [-u user] {-l-r-e} the first format is used to install a new crontab file, installation of chunfade? If the "-" symbol is used as the file name, it means that the standard input is used as the installation source. -U if this option is used, that is, the crontab file of the specified user will be modified. If this option is not specified, crontab is the crontab of the operator by default, that is, the crontab file of the user executing the crontab command will be modified. However, if you use the su command and then use the crontab command, there may be confusion. Therefore, if the su command is used, it is best to use the-u option to specify the user's crontab file. -L display the current crontab on the standard output. -R: delete the current crontab file. -E use the EDITOR referred to by VISUAL or EDITOR environment variables to edit the current crontab file. After editing is completed, the edited files are automatically installed. [Example 7] # crontab-l # list the current crontab of a user. 10 6 * date 0/2 * date 0 23-7/2, 8 * date # How to enter the command and time to be executed in the crontab file. Each line in this file contains six fields, the first five of which specify the time when the command is executed, and the last field is the command to be executed. Each domain is separated by spaces or tabs. The format is as follows: minute hour day-of-month-of-year day-of-week commands the first item is minute, the second item is hour, and the third item is the day of a month, the fourth item is the month of the year, the fifth item is the day of the week, and the sixth item is the command to be executed. These items cannot be blank and must be filled in. If you do not need to specify several items, you can use * instead. Because * is a unified character and can replace any character, it can be considered as any time, that is, this item is ignored. The valid range of each item is given in Table 4-1. Table 4-1 valid time range of specified time minute hour day-of-month-of-year day-of-week valid value 00-59 00-23 01-31 01- 12 0-6 (0 is Sunday) in this way, you can write unlimited lines to the crontab file to complete unlimited commands. All commands and symbols that can be written in the command line can be written in the command domain, and other time domains can be listed, that is, many time values can be written in the domain, if any of these time values is met, execute the command, and use commas to separate each two time values. In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers. Example: at every morning, *** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox. 0 */2 *** echo "Have a break now every two hours. ">/tmp/test.txt every two hours from PM to AM, 8 *** echo "Have a good dream :)">/tmp/test.txt AM, AM, Am- 3 command line a.m. on April 9, January 1, 4, 1, 1 * command line SHELL =/bin/bash PATH =/sbin: /bin:/usr/sbin:/usr/bin MAILTO = root // if an error occurs or data is output, the data is sent to the account HOME as the path for running. here is the root directory # run-parts 01 ***** root run-parts/etc/cron. hourly // Each Run/etc/cron in hours. 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. monthly // 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. This indicates the calendar month of any day, in fact, the command is executed every day at four o'clock P.M., 15 min, 25 min, 35 min, 45 min, and 55 min., 17, 18 *** the system enters the maintenance status at every Monday, 3, and 5, and restarts the system. In the crontab file, you should write the following fields: 00 15 **, 5 shutdown-r + 5 and then save the file as foxy. cron, and then type crontab foxy. cron to install the file. Execute the innd/bbslin command in the user directory at 10 and 40 hours: *** innd/bbslink executes the bin/account command in the user directory every hour: 1 *** bin/account execute the following two commands in the user directory at 03:20 every morning (each command is separated ): 20 3 *** (/bin/rm-f expire. ls logins. bad; bin/expire $ # @ 62; expire.1st) January and April of each year, run the/bin/rm-f expire.1stcommand from 03:12 to 03:55 on the 9th, and paste the result to the end of the mm.txtfile (the mm.txt file is located in your own directory ). 3 4-9 */bin/rm-f expire.1st$#@62;$#@62;mm.txt crontab command: 1 man cron 2 man crontab 3 man 5 crontab => there is a 5 in the middle. Command "crontab-e": export PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin # This is a blind spot, I never thought about it, but I was disappointed again and again. [Separated by colons] DISPLAY =: 0.0 # add it. a colon has a period. # Minute, day, month, week, Command 1 23, 1 8 * shutdown-h + 3 # shutdown at on January 1, (delay of 3 minutes), 7 23 1 8*#1 minute, 4 points, 7 points [January 8] */3 23 1 8 * #, 6, 9... every 3 minutes (or every 3 minutes, a little different from every 3 minutes) [1-10]/3 23 1 8*#1 minute, 4 minutes, 7 minutes [from 1 minute, add three minutes one by one until the value is greater than 10 ==> This is the correct solution] @ reboot shutdown-k now # this line can scare you, shut down when the server is started (if "-k" is changed to "-h ). "@ Reboot" indicates execution at startup. --------------------------------- END -------------------------------------------- another script: **************************************** ***************************************[ 0-59]/5 23 *** shutdown-h now # shut down every five minutes at every night, the desire is good, but it will not be executed, because no PATH variable is set. for the solution, see the downstream. [0-59]/5 23 ***/sbin/shutdown-h now # The path is provided to ensure that it is shut down. 0 6 1 8 * xmms/music/zhangchu/lightweight # Call me a song at six o'clock tomorrow morning. However, I was disappointed that I still didn't hear the music until seven o'clock. [DISPLAY =: 0.0]. The last point of this line is added: Currently, the 99% motherboard supports timed boot. if there is no alarm, you can ask the computer to wake you up. the option is [wake up by alarm] in the BIOS power supply. * END ****** ************************************* can be freely modified, it was initially released on the Linuxsir website. Let's take a look at a super user's crontab File: # Run the 'atrun' program every minutes # This runs anything that's due to run from 'at '. see man 'at' or 'atrun '., 20, 25, 30, 35, 40, 45, 50, 55 */usr/lib/atrun 40 7 * updatedb 8, 10, 54,58 ****/bin/sync
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.