Linux cron and crontab

Source: Internet
Author: User
Linux cron and crontab croncrond are located in/etc/rc. d/init. d/crond or/etc/init. d or/etc/rc. d/rc5.d/S90crond, always reference/var/lock/subsys/crond. Cron is a scheduled execution tool in linux (equivalent to cron and crontab cron crond in Linux in windows are located in/etc/rc. d/init. d/crond or/etc/init. d or/etc/rc. d/rc5.d/S90crond, always reference/var/lock/subsys/crond. Cron is a scheduled execution tool in linux (equivalent to scheduled tasks in windows). It can regularly run task tasks without manual intervention. Since cron is a Linux service (deamon), you can start or disable this service in the following ways: /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 it. The two crontab is located in/usr/bin/crontab. The cron service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command: crontab-u // Set the cron service of a user, 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>/tmp/ls.txt the first part of the format sets the time, and the subsequent part is the command to be executed. if you want to execute There are too many commands. 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 the number from a number to a number, "," separate several discrete numbers. The following example illustrates the problem: 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, 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 modification. The system level configuration file of the/etc/crontab configuration file cron is located in/etc/crontab. The cron service reads not only all files in/var/spool/cron every minute, but also the/etc/crontab configuration file once, therefore, we can use the cron service to configure this file. The configuration with crontab-e is for a user, while editing/etc/crontab is for system tasks. 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 // execute every month /Etc/cron. 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. Four instances -------------------------------------- basic format: [parameters must be separated by spaces] ***** command minute-by-day, month-and-week command column 1st indicates minute 1 ~ 59 every minute, use * or */1 to indicate 2nd columns to indicate hour 1 ~ 23 (0 indicates 0 points) column 3rd indicates date 1 ~ 31 column 4th indicates the month 1 ~ 12 column ID No. 0 ~ 6 (0 indicates Sunday) some examples of the command crontab file to be run in column 6th: 30 21 ***/usr/local/etc/rc. d/lighttpd restart the above example indicates restarting lighttpd at every night. 45 4, 22 **/usr/local/etc/rc. d/lighttpd restart the preceding example indicates restarting lighttpd at on the first, tenth, and 22th days of each month. 10 1 ** 6, 0/usr/local/etc/rc. d/lighttpd restart the above example indicates restarting lighttpd at every Saturday and Sunday. The preceding example of-23 ***/usr/local/etc/rc. d/lighttpd restart indicates that lighttpd is restarted every 30 minutes between to every day. 0 23 ** 6/usr/local/etc/rc. d/lighttpd restart the above example indicates that the lighttpd is restarted at pm every Saturday. **/1 ***/usr/local/etc/rc. d/lighttpd restart lighttpd * 23-7/1 ***/usr/local/etc/rc every hour. d/lighttpd restart: restart lighttpd every one hour from to AM. 0 11 4 * mon-wed/usr/local/etc/rc. d/lighttpd restart 4 of each month and restart lighttpd 0 4 1 jan */usr/local/etc/rc from every Monday to Wednesday. d/lighttpd restart lighttpd 5 special usage at on January 1, January 1 @ hourly/usr/local/www/awstats/cgi-bin/awstats. sh use @ hourly ??? NE 0 ****,? You can use string meaning ------ ------- @ reboot Run once, at startup. @ yearly Run once a year, "0 0 1 *". @ annually (same as @ yearly) @ monthly Run once a month, "0 0 1 **". @ weekly Run once a week, "0 0 ** 0 ". @ daily Run once a day, "0 0 ***". @ midnight (same as @ daily) @ hourly Run once an hour, "0 ****".
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.