Inux's cron and crontab

Source: Internet
Author: User

A cron

Crond is located in/etc/rc.d/init.d/crond or/ETC/INIT.D or/etc/rc.d/rc5.d/s90crond, the most general reference is/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 service for Linux (Deamon), you can start and shut down the services in the following ways:
/sbin/service Crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration

You can also start the service automatically when the system starts:
At the end of the/etc/rc.d/rc.local script, add:
/sbin/service Crond Start

Now cron This service is already in the process, we can use this service.

Two crontab

Crontab is located in/usr/bin/crontab.

The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:
Crontab-u//Set a user's Cron service, which is usually required by the root user when executing this command
CRONTAB-L//list details of a user cron service
Crontab-r//Delete a user's cron service
CRONTAB-E//Edit a user's cron service


For example, root to view your cron settings: Crontab-u root-l
Again, 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 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 time setting, 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 recall the full path of the command when the call. Time setting we have a certain agreement, the preceding five * number represents five numbers, the value range and meaning of the numbers are as follows:

Minutes (0-59)
Hours (0-23)
Date (1-31)
Month (1-12)
Week (0-6)//0 on behalf of Sunday

In addition to the numbers there are several special symbols are "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" represents from a number to a number, "," separate several discrete numbers. Here are a few examples to illustrate the problem:

Every morning at 6.
0 6 * * echo "Good morning." >>/tmp/test.txt//Note simply Echo, no output is visible from the screen, because cron emails any output to root.

Every two hours
0 */2 * * echo "has a break now." >>/tmp/test.txt

Every two hours between 11 o'clock and 8 in the morning, eight in the morning.
0 23-7/2,8 * * * echo "a good Dream:)" >>/tmp/test.txt

Every month, number 4th and Monday to Sunday, three a.m., 11.
0 4 * 1-3 command line

January 1 morning, 4.
0 4 1 1 * command line

After editing a user's cron settings, Cron automatically generates a file with the same name as the user under/var/spool/cron, and the cron information for this user is recorded in this file, which cannot be edited directly and 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.

Three edit/etc/crontab configuration file
The system-level configuration file for 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 an email
home=///user-run path, here is the root directory

# Run-parts

* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily//daily execution of scripts within/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly//weekly execution of scripts within/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute scripts within/etc/cron.monthly

Attention to the "run-parts" This parameter, if you remove this parameter, you can later write to run a script name, not the folder name.

Four examples

--------------------------------------

Basic format: [parameters must be separated by spaces]
* * * * * command
Time-sharing Weekly command

The 1th column represents minutes 1~59 per minute with * or */1
The 2nd 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
5th Column Identification Number Week 0~6 (0 = Sunday)
6th List of commands to run

Some examples of crontab files:

* * * * */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 a 4:45 restart lighttpd per month for 1, 10, 22nd.

1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows a 1:10 restart lighttpd per Saturday and Sunday.

0,30 18-23 * * */usr/local/etc/rc.d/lighttpd restart
The example above shows that LIGHTTPD restarts 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 the LIGHTTPD is restarted every Saturday.

* */1 * * * */usr/local/etc/rc.d/lighttpd restart
Restart LIGHTTPD every hour

* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart
From 11 o'clock to 7 in the morning, restart lighttpd every hour.

0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month and 11-point restart lighttpd from Monday to Wednesday

0 4 1 Jan */usr/local/etc/rc.d/lighttpd restart
January 1 4-point restart LIGHTTPD

Five special usages

@hourly/usr/local/www/awstats/cgi-bin/awstats.sh Use @hourly the response is 0 * * *, and the following can be used: string meaning------ -------@reboot Run once, at startup @yearly Run once a year, "0 0 1 1 *". @annually (Same as @ye Arly) @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 * * * *". Transferred from: http://www.cnblogs.com/itech/archive/2011/02/09/1950226.html

Inux's cron and crontab

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.