Scheduled Tasks in Linux
Executes the specified task within the agreed time, called a scheduled task.
There are generally two ways to schedule tasks in Linux:
Scheduled Tasks and recurring scheduled tasks that are performed at once
One-time scheduled tasks, using command at
a at: Perform a specified task at a specified time in the future ;
It is used in the format: at [option] ... Time
Time is specified.
(1) Absolute time
hh:mm
Mmdd[cc]yy,
Mm/dd/[cc]yy,
Dd.mm.[cc]yy or [Cc]yy-mm-dd
Example 1:650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M02/72/DD/wKioL1Xu_UmQQJMgAABzEoaNwVI956.jpg " Title= "At1.png" alt= "Wkiol1xu_umqqjmgaabzeoanwvi956.jpg"/>
Submit the task using Ctrl+d after the input is complete
(2) Relative time
now+ #UNIT The #unit time after the current time
Unit: Minute, hour, day, week
Example 2:650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M00/72/DD/wKioL1Xu_fyxZES9AAB4imlfbMc427.jpg " Title= "At2.png" alt= "Wkiol1xu_fyxzes9aab4imlfbmc427.jpg"/>
(3) Blur time
Midnight,noon,teatime,tomorrow,minute, hour, day, week
[option] Options for command execution
Common options:
-L: View a list of such running jobs in the job queue; equivalent to using the ATQ command;
-C At_job_num: Look at the contents of the running job, At_job_num is the job ID number;
-Q queue:at Job queue;
-D: Delete the specified job; equivalent to ATRM
Example 3:650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M01/72/DD/wKioL1XvACjxqua6AAEyEX8Yoqc561.jpg " Title= "At3.png" alt= "Wkiol1xvacjxqua6aaeyex8yoqc561.jpg"/>
-f/path/from/somefile: Reads the job to be run from the specified file;
Two recurring tasks
There are two types of recurring tasks:
(1) System cron task; There is no default running user identity, so you need to specify the additional runner;
Task File/etc/crontab program is edited by default using Vim
# * * * * * * user-name command to be executed
First 5 fields: Time points, respectively, day and week
After 2 fields:
User-name: Run the task as a user
command to being executed: to run a task
(2) User Cron Task: Submitted by a user, the default is to run as the submitter, so there is no need to specify additional
run by;
/var/spool/cron/username
# * * * * * command to be executed
5 Time points:
Minutes: valid value range 0-59;
Hours: 0-23
Day: 1-31
Month: 1-12
Weeks: 0-7
Note: The date of the month and the number of weeks, not recommended to use at the same time;
Time notation:
*: Each time point in the valid value range of the time point;
-: A specific continuous time range, 3-7
,: A discrete point in time, 3,5,7
/#: The amount of time in a valid time frame, for specifying the frequency;
(3) Example/bin/echo * * * * * * "Hello world!"
Represents 15:30 of daily tasks
Example * * * * 4/bin/echo "Hello world!"
Represents 15:30 per Thursday of performing tasks
Example 1-4 * */bin/echo "Hello world!"
Represents 15:30 missions per Monday through Thursday
Example 3,7 * */bin/echo "Hello world!"
Represents the 15:30 missions of 3rd and 7th each month.
Example 5:*/6 * * * */bin/echo "Hello world!"
Indicates that a scheduled task is executed every 6 minutes
Example 6:1 */4 * * */bin/echo "Hello world!"
Indicates that a scheduled task is performed every 4 hours, and if the minute field is *, it is reached
Every minute of the hour is executed.
(4) crontab command:
crontab [-u user] [-l |-r |-e]
-u User: Not managing your own cron task, but specifying the target user's cron task; root only
Manage the permissions of other users ' cron tasks, and manage their own by default;
-l:list, list the tasks;
-r:remove to remove all tasks;
-e:edit, edit, open a default editor for the current shell session to edit the cron task table;
Homework Exercises:
1, every Monday to Saturday 3:20 A.M., run the CP command to archive the/etc/directory, storage location is/BACKUPS/ETC-YYYY-MM-DD;
2 0 3 * * 1-6/usr/bin/cp-r/etc//backups/etc-$ (date + '%y-%m-%d ')
2, every Sunday 2:30 A.M., run the CP command to backup the/etc/fstab file, the storage location is/BACKUP/FSTAB-YYYY-MM-DD-HH-MM-SS;
3 0 2 * * 0/usr/bin/cp/etc/fstab/backups/etc-$ (date + '%y-%m-%d-%h-%m-%s ')
3, every night 12 o'clock, get all the lines in the/proc/meminfo file starting with S or M, appended to the/statistics/meminfo.txt file, and the daily message before, to add a similar =============== separator line;
0 0 * * * echo "==============================================" >> meminfo.txt &&/usr/bin/egrep "^ (s| M) "/proc/meminfo >> meminfo.txt
Linux Scheduled Tasks