Crontab Task Scheduling

Source: Internet
Author: User
Cron is a regular execution tool under Linux that can run jobs without human intervention. Because Cron is a built-in service for Linux, it does not automatically get up and you can start and close this service in the following ways:

/sbin/service crond Start//boot service
/sbin/service Crond Stop//Off service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration

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

Now that the Cron service is in the process, we can use this service, and the Cron service provides the following interfaces for everyone to use:


1. Edit directly with crontab command

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 required by the general 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

For example, root 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 a cron service, there are some formatting and conventions for editing the content, input: Crontab-u root-e
into the vi editing mode, the contents of the edit must conform to the following format: */1 * * * * ls >>/tmp/ls.txt

The first part of this format is the set of time, the next part is the command to execute, if there are too many commands to execute, you can write these commands into a script, and then call the script here directly, and then remember to write the full path of the command. Time set we have a certain agreement, the front five * number represents five digits, the number of the value range and the meaning is 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 a few special symbols is "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" representing 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 that pure echo does not see any output from the screen because cron emails any output to Root's mailbox.

Every two hours

0 */2 * * echo "Have a break now." >>/tmp/test.txt

Between 11 o'clock and 8 in the morning, every two hours, eight in the morning.

0 23-7/2,8 * * * echo "Have a Good Dream:)" >>/tmp/test.txt

4th a month and a week on Monday to three in the morning 11.

0 4 * 1-3 command line

January 1 morning, 4.

0 4 1 1 * command line

After each edit of a user's cron settings, Cron automatically generates a file with the same name as this user under/var/spool/cron, and the user's cron information is recorded in this file, which is not directly editable and can only be edited with CRONTAB-E. After Cron starts, read the file every time it is written, and check if you want to execute the commands inside. Therefore, you do not need to restart the Cron service after this file has been modified.

2. Edit/etc/crontab File Configuration cron

The cron service does not only have to read all the files in the/var/spool/cron every minute, but also read the/etc/crontab, so we can configure the file to do something with the cron service. The crontab configuration 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 there is an error, or if there is data output, the data is sent to this account as mail

home=///user Run path, this 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/etc/cron.daily scripts
4 * * 0 root run-parts/etc/cron.weekly//per week execute/etc/cron.weekly script
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute/etc/cron.monthly script

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


--------------------------------------
Basic format:
* * * * command
Time-sharing and Lunar Week command


The 1th column represents minutes 1~59 per minute with * or */1
The 2nd column represents the hour 1~23 (0 for 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
The 5th list of the week 0~6 (0 for Sunday)
6th column the command to run

Some examples of crontab files:

* * * */usr/local/etc/rc.d/lighttpd restart
The above example shows a 21:30 reboot lighttpd per night.


4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example represents the 4:45 restart LIGHTTPD for 1, 10, and 22nd of each month.


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


0,30 18-23 * * * */usr/local/etc/rc.d/lighttpd restart
The example above indicates 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 the 11:00 PM restarts lighttpd every Saturday.


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


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


0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month with 11 points per Monday to Wednesday restart LIGHTTPD


0 4 1/USR/LOCAL/ETC/RC.D/LIGHTTPD * Restart
Restart lighttpd at 4 o ' January 1.


=========================================
Crontab-e


Enter a VI editing interface


On the last line, add


*/30 * * * * netstat >/tmp/net.log


The netstat command is executed every 30 minutes and the execution result is deposited in net.log.


Crontab is a handy program to perform a task on a unix/linux system on a regular basis (loop)


Use the Cron service to view the Cron service status with service Crond State, or service Crond start if it is not started,


A cron service is a timed service that can be added to or edited by the crontab command to perform tasks that need to be performed regularly:


Crontab-u//Set a user's Cron service, which is required by the general root user when executing this command


CRONTAB-L//list details of a user's cron service


Crontab-r//Remove cron service for no users


CRONTAB-E//Edit a user's cron service


For example, root 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 a cron service, there are some formatting and conventions for editing the content, input: Crontab-u root-e


into the vi editing mode, the contents of the edit must conform to the following format: */1 * * * * ls >>/tmp/ls.txt


Edit the/etc/crontab file with one line at the end: 5 * * * Root init 6 This configures the system to automatically restart at 5-point 30 per morning.


You need to set up the Crond service that starts automatically after the system starts, and can be added at the end of the/etc/rc.d/rc.local


Service Crond Start


If you also need to load additional services at the system start ten, you can continue to add the start command for other services.


For example: Service mysqld start


Basic usage:


1. Crontab-l


List the current crontab tasks


2. crontab-d


Delete the current crontab task


3. CRONTAB-E (solaris5.8 above is crontab-r)


Edit a crontab task, Ctrl_d end


4. crontab filename


Task list file with filename as crontab and load


Format of crontab file:


The rows in the crontab file are made up of 6 fields, separated by spaces or tabs between the different fields. First 5 fields specify when the command will run


Minutes (0-59)


Hours (0-23)


Date (1-31)


Month (1-12)


Days of the week (0-6, of which 0 represent Sunday)


The 6th field is a string to be executed at the appropriate time


Example:


#MIN HOUR Day MONTH DayOfWeek COMMAND


#每天早上6点10分


6 * * * Date


#每两个小时


0 */2 * * * DATE (Solaris 5.8 does not seem to support this type of notation)


#晚上11点到早上8点之间每两个小时, 8 in the morning.


0 23-7/2,8 * * * Date


#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点


0 4 * mon-wed date


#1月份日早上4点


0 4 1, * date


Add: When using crontab, pay special attention to the environment variables that can be accessed in the run script and the environment variables in the current test environment are not necessarily the same, a more insurance method is to set up the environment variable (export) in the running script program.


(1) First build a file Crond.txt as follows, every 5:36 A.M. reboot


5 * * * reboot


(2) Upload to/opt directory


(3) Run command


Crontab/opt/crond.txt


Crontab-l


Make the configuration file effective: If you have the configuration file in effect, you will have to restart Cron, remember that since the Cron profile is modified under each user. You also need to restart the cron server.


In Fedora and redhat, we should use;


[Root@localhost ~]#/etc/init.d/crond Restart


If you allow Crond to run at boot time, you should change its operating level;


[Root@localhost ~]# chkconfig--levels Crond on


Service Crond Status View Cron service state, if not started service Crond start it, cron service is a timed service, can add or edit tasks that need to be performed by Crontab command


Each line of the crontab file consists of six domains (minutes, hours, day of month, month, Day of Week, command), separated by spaces or tabs, where:


Minutes: Minutes field, the range of values is 0 to 59


Hours: The hour field, the range of values is 0 to 23


Day of Month: date, value range is 1 to 31


Month: month, the range of values is 1 to 12


Day of week: Week, values range from 0 to 6, Sunday value is 0


Command: Commands to run


If a field is *, it indicates that the command can be executed in all possible ranges of the domain.


If a field is two digits separated by hyphens, the command can be executed within the range of two digits (including two digits themselves).


If a field is made up of a series of values separated by commas, the command can be executed within the range of these values.


Both fields are valid if both the Date field and the week field have values.


Write a file to start the automatic backup process.


Cd/opt


Touch Reboot.txt


Add content to Reboot.txt:


0 4 * * * reboot


Crontab/opt/reboot.txt


Edit a timed operation with CRONTAB-E, such as adding a downlink command:


Use the crontab-l command to view


Note: The service needs to be started (added in rc.local)


Restart Crond Task


/etc/init.d/cron restart (under Ubuntu)


The first type is in the system of the RPM package management, such as Fedora or redhat;


[Root@localhost ~]#/etc/init.d/crond start


[Root@localhost ~]#/etc/init.d/crond Stop


[Root@localhost ~]#/etc/init.d/crond Restart


/etc/rc.d/init.d/crond restart


Introduction to Commands


crontab-the daemon for each user and the schedule for the execution.


Partial parameter description


crontab file [-u user]-replaces the current crontab with the specified files.


Crontab-[-u user]-replaces the current crontab with standard input.


crontab-1[user]-lists the user's current crontab.


crontab-e[user]-Edit user's current crontab.


crontab-d[user]-deletes the user's current crontab.


Crontab-c dir-Specifies the directory for crontab.


crontab file format: M H d M D cmd.


M: Minutes (0-59).


H: Hours (0-23).


D: Days (1-31).


M: Month (1-12).


D: Days of the week (0~6,0 for Sunday).


CMD to run the program, the program was sent to SH execution, this shell only user,home,shell these three environment variables.


Here is an example file:


#MIN HOUR Day MONTH DayOfWeek COMMAND


#每天早上6点


106* * * Date


#每两个小时


0*/2* * * Date


#晚上11点到早上8点之间每两个小时, Early Upper point


0 23-7/2,8* * * Date


#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点


0 4* mon-wed Date


#1月份日早上4点


0 4 1 jan* date


Example


LARK:~>CRONTAB-1 lists the user's current crontab.


#MIN HOUR Day MONTH DayOfWeek COMMAND


Ten 6* * * Date


0*/2* * * Date


0 23-7/2,8 * * * Date


Lark:~>



Use rights: Root and Crontab file owner,
Grammar:
crontab [E [Username]|-l [Username]|-r [Username]|-v [username]| File]
Reboot: Sudo/etc/init.d/cron Restart
Description
Crontab is used to allow the user to execute a program at a fixed time or at a fixed interval, in other words, a user-like schedule. -u user is the time table that specifies the user, as long as you have permission (for example, root) to specify the schedule for others. If you do not use-u user, it means setting your own schedule.
Parameters:
-E [UserName]: Execute the text editor to set the time table, the default text editor is VI, if you want to use another text editor, please set the VISUAL environment variables to specify the use of that text editor (for example, Setenv VISUAL Joe)
-R [UserName]: Deletes the current schedule table
-L [UserName]: List the current schedule
-V [UserName]: Lists the status of a user cron job
The format of the schedule table is as follows:
F1 F2 F3 f4 f5 program
Where F1 is the minute, F2 represents the Hour, F3 represents the day of the month, the F4 represents the month, and the F5 represents the day of the one week. Program represents the code to be executed, which is the command written at the terminal.
When F1 is *, the PROGRAM,F2 is executed every minute for *, which means that the program is executed hourly, and the rest of the analogy
When F1 is a-b to be executed from the time of the first a minute to the B minute, the F2 is a-b to execute from a to B hours, and the rest by analogy
When F1 is */n, it is executed once every n minutes, F2 is performed once per N-hour intervals for */n, and the rest by analogy
When F1 is a, B, C,... The first A, B, C,... Minutes to execute, F2 for a, B, C,... The first is a, B, c ... An hour to execute, the rest of the analogy
The user can also store all the settings in the file file, using crontab file to set the schedule.
Because of the different versions of UNIX, there are some differences in syntax, such as setting interval execution in HP UNIX AIX If syntax errors occur in the */n way, in this type of UNIX, the interval execution can only be enumerated, see examples.
How to use:
Edit a file Cronfile with VI and enter a well-formed schedule in this file. When the edit is complete, save and exit.
Entering at the command line
$: crontab cronfile
This submits the Cronfile file to the C R o N process, and a copy of the newly created cronfile has been placed in the/V a r/s p o l/c r n Directory, and the filename is the user name.
Example:
Perform a/bin/ls every 0 minutes per day of the month:
0 * * * */BIN/LS
In December, every 6 to 12 o ' Day in the morning, every 20 minutes to perform a/usr/bin/backup:
*/20 6-12 */usr/bin/backup
Send a letter to Alex@domain.name from Monday to Friday every 5:00:
0 * * 1-5 mail-s "HI" Alex@domain.name </tmp/maildata
Every month at midnight 0:20, 2:20, 4:20 .... Execute echo "haha"
0-23/2 * * * echo "haha"
Between 11 o'clock and 8 in the morning, every two hours, 8 in the morning.
0 23-7/2,8 * * * Date
In HP Unix, in every 20 minutes, the expression is: 0,20,40 * * * * * * * and cannot be */n mode, otherwise there will be syntax errors

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.