If we need to perform a task at a certain time, or repeat a task in a fixed cycle, in a routine or use of a computer, we need to set a scheduled task on the system; Linux system, we can use a one-time scheduled task at and the Batch command, or recurring scheduled tasks crontab .
一、一次-Sex Task execution:
1 , command format:
#at [option] Time
2 , Time format:
Specific time:
hh:mm: where HH is the hour andMM is minutes.
MMDDYY,mm/dd/yy: where MM is the minute,DD is the first day,YY refers to the year.
Now+unit: where unit units can be minutes ,hours,days ,weeks.
Blur Time:
Today,noon,midnight,teatime,tomorrow
3 , at Task format:
#at time
At> COMMAND
Ctrl+d: Submitting a task
At with job queue: using a single letter representation
At–l,ATQ: View pending tasks in the job queue
At-d,atrm: Delete Task.
After the task job execution completes: The results are notified to the job submitter by mail
CentOS: The Mail Service is installed and started by default and only serves the local system;
#batch: The function is the same at, but no time is required for it, it chooses to perform the specified job when the system resource is more idle
Second, Recurring task execution:
1 , install and start the service:
packages that need to be installed:Crond
#yum Install Cronie
Check crond:
#/etc/init.d/crond status
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/BB/wKiom1WD33DiqxlmAABOX9QsLMI788.jpg "title=" 1.jpg " alt= "Wkiom1wd33diqxlmaabox9qslmi788.jpg"/>
If the status information isstopped, you need to start crond:
#/etc/init.d/crond start
check crond boot up:
#chkconfig –list Crond
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/B7/wKioL1WD4VeRzQOCAABSgmZfwQ8711.jpg "title=" 2.jpg " alt= "Wkiol1wd4verzqocaabsgmzfwq8711.jpg"/>
If you need to set up boot Crond for non-boot boot :
#chkconfig Crond on
2 , Cron Task Category:
system Cron: defined in /etc/crontab;
user cron: defined in the/var/spool/cron directory, each user has a file with the same name as the user name, which functions like /etc/crontab.
3 , crontab How to use:
How user cron tasks are defined:
Crontab
-uusername: Specify user, no- u Select Xiangme think current user.
-L: lists all tasks that have been defined
-E: Open the Edit Interface definition task
-r: Remove All Tasks
#crontab-U username–e
The/var/spool/cron/ directory can only be read and written by the root user, and normal users can edit tasks using the crontab-e command;
4 , crontab format:
Execution #crontab-E , there will be Vim Editor for task editing;
Format Description:
#vim/etc/crontab
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/B7/wKioL1WD4X-y4hgcAAD6gd34mV8907.jpg "title=" 3.jpg " alt= "Wkiol1wd4x-y4hgcaad6gd34mv8907.jpg"/>
Format:
Cron needs to specify environment variables when executing task commands, such as SHELL,PATH,HOME,MAILTO, and so on;
The left represents the time, and the right side is the executed command;
Each row defines a separate task:
Time:
The time is composed of 5 numbers, representing: minutes (0-59), hours (0-23), date (1-31), month (1-12), Week ( 0-6).
Special symbols:
*: any number;
/#: Each unit of #;
M–n: From M to N
A,b,c,... : A,b,c and many more numbers.
Time notation:
(1), each time bit should use the value within its available valid range of values
3 * * * | 17 3 * * | 7 6 5 * * | 7 6 5 4 *
(2), the * on a time bit indicates all valid values for the corresponding time bit;
1 9 * 3, 9 points 1 centsper Wednesday .
(3),*/#: in the corresponding time position of the effective value of each # once;
*/3 * * *: Every 3 hours;
0 */3 * *: Every 3 hours on the hour;
*/8 * * * *: Every 8 minutes , 0-59:0, 8, +,, +,--
(4), a continuous interval on a time position:-
10-50/3 * * *: from ten to 3 minutes each ;
(5), Discrete value:
1 7,9,13 * *:7:01,9:01,13:01
Command:
if the task defined in cron is a script, each command in this script looks for the cron PATH environment variable definition, and therefore, tests the problem-free script at the command line, in cron may not be in;
Solution:
the command uses absolute paths, and in the script begins to define a private PATH environment variable.
if you use% in a crontab user command , youhave to escape using the \% format, but you can use single quotation marks instead of escaping;
5 3 * * */bin/touch/tmp/testfile-$ (date+\%f-\%h-\%m-\%s)
6 4 * * */bin/touch/tmp/testfile-$ (date+ '%f-%h-%m-%s ')
To perform a task:
after the task job execution finishes:
Each execution result will be notified to the job submitter by email;
Discard Mail Notifications:
Output redirection:
>/dev/null
&>/dev/null
>/dev/null 2>&1
5. Supplement:
How to implement a second-level task:
every ten seconds:
* * * * * for i in {1..5}; Do echo "Howdy"; sleep 10; Done
0
10
20
30
50
0
complementary mechanisms of Anacron:crontab:
Check for tasks that have not been performed in the past for a valid period, and if so, at a specified point in time after boot.
This article is from the "Pony Learning Record" blog, make sure to keep this source http://masachencer.blog.51cto.com/8683770/1663673
Linux Scheduled Tasks