Cron and crontab for Linux

Source: Internet
Author: User

A croncrond is located in/etc/rc.d/init.d/crond or/ETC/INIT.D or/etc/rc.d/rc5.d/s90crond, the most general reference to/var/lock/subsys/crond. Cron is a timed execution tool under Linux (equivalent to scheduled task under Windows) that runs task tasks on a scheduled basis without human intervention. Since Cron is a Linux service (Deamon), you can start and shut down this service in the following ways:/sbin/service crond start//start service/sbin/service Crond stop//Shut down service/sbin/ Service Crond restart//restart Services/sbin/service Crond Reload//Reload configuration You can also start the service automatically when the system starts: at the end of the script, add:/ Sbin/service Crond Start now cron This service is already in the process, we can use this service. The two crontabcrontab are located in/usr/bin/crontab. The Cron service provides the crontab command to set the Cron service, and here are some parameters and instructions for this command: crontab-u//Set a user's Cron service, which is typically required by the root user when executing this command crontab-l// List details of a user's Cron service crontab-r//delete a user's Cron service crontab-e//Edit a user's Cron service such as root to view his cron settings: Crontab-u root-l For example, Root wants to delete Fred's cron settings: crontab-u fred-r When editing the cron service, the edited content has some formatting and conventions, input: Crontab-u root-e into the vi editing mode, the content of the edits must conform to the following format: */1 * * * * ls > >/tmp/ls.txt The first part of this format is the setting of the time, 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 remember to write the full path of the command. Time setting we have a certain agreement, the first five * number represents five numbers, the value range and the meaning of the numbers are as follows: minutes (0-59) hours (0-23) Date (1-31) month (1-12) weeks (0-6)//0 for Sunday In addition to the numbers there are several special symbols that are "*", "/" and "-", ",", * representing all the values within the range of numbers, "/ "On behalf of each meaning," */5 "represents every 5 units,"-"represents a number from one digit to a number,", "separates several discrete numbers. Here are a few examples to illustrate the problem: 6 a.m. 0 6 * * * echo "Good morning." >>/tmp/test.txt//Note simply Echo, there is no output from the screen because cron email any output to Root's mailbox The Every two hours 0 */2 * * * echo "has a break now." >>/tmp/test.txt 11 o'clock to 8 a.m. every two hours, morning eight points 0 23-7/2,8 * * * echo "have a goo D dream:) ">>/tmp/test.txt 4th per month and Monday to Sunday three a.m. 11 0 4 * 1-3 command line January 1 morning 4 point 0 4 1 1 * command line every time you finish editing a Cron settings for a user, Cron automatically generates a file with the same name as this user under/var/spool/cron, and this user's cron information is recorded in this file, which cannot be edited directly, but 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. The system-level configuration file for the three-edit/etc/crontab profile Cron is located in/etc/crontab. Cron service every minute not only to read all the files within/var/spool/cron, but also to read the/etc/crontab configuration file once, so we configure this file can also use the Cron service to do something. The configuration made with CRONTAB-E is for a user, and editing/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 mail home=/ User Run path, here is the root directory # run-parts * * * * root run-parts/etc/cron.hourly//hourly Execute script within/etc/cron.hourly 4 * * * Root run-  parts/etc/cron.daily//Daily Execute script within/etc/cron.daily 4 * * 0 root run-parts/etc/cron.weekly//weekly script executed within/etc/cron.weekly 42 4 1 * * Root run-parts/etc/cron.monthly//every month to execute/etc/cron.monthly within the script you notice "run-parts" This parameter, if you remove this parameter, you can write a script name to run, Instead of the folder name. Four instances--------------------------------------basic format: [parameters must be separated by a space]* * * * * command time and Moon Week order 1th indicates minutes 1~59 per minute * or */1 The 2 column represents the hour 1~23 (0 means 0 points) The 3rd column represents the date 1~31 the 4th column represents the month 1~12 the 5th column identification Number week 0~6 (0 means Sunday) 6th column to run some examples of the command crontab file: * * */usr/local/etc/rc.d/ LIGHTTPD Restart the above example shows a 21:30 restart lighttpd per night. 4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD Restart the above example shows 4:45 restart lighttpd per month for 1, 10, 22nd. 1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD Restart the above example shows the 1:10 restart lighttpd per Saturday and Sunday. 0,30 18-23 * * * */USR/LOCAL/ETC/RC.D/LIGHTTPD Restart the above example shows that the LIGHTTPD is restarted every 30 minutes from 18:00 to 23:00 every day. 0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD Restart the above example indicates that each Saturday's 11:00am restarts lighttpd. * */1 * * */usr/local/etc/rc.d/lighttpd restart every hour restart lighttpd* 23-7/1 * */usr/local/etc/rc.d/lighttpd restart 11 o'clock to 7 in the morning. Between, every hour restarts lighttpd0 4 * mon-wed/usr/local/etc/rc.d/lighttpd Restart monthly 4th and 11 points per Monday to Wednesday restart lighttpd0 4 1 Jan */usr/local/e TC/RC.D/LIGHTTPD Restart January 1 4-point restart LIGHTTPD Five special usage @hourly/usr/local/www/awstats/cgi-bin/ use @hourly corresponds to 0 * * * *, the following can be used: string meaning-------------@reboot Run once, at startup. @yearly Run Once  A year, "0 0 1 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 * * * * *". Finish!

Cron and crontab for Linux

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: 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.