Note: The results of each execution of a Linux task plan, including errors, will be sent to the administrator in message format, so you must install the Mail service to view the results of the task execution
1. E-Mail Service:
Smtp:simple Mail Transmission protocol
/var/spool/mail/username
Pop3:post Office Protocol
Imap4:internet Mail Access Protocol
Mail command:
Mail: Enter the interactive email interface;
Mail-s ' SUBJECT ' [email protected]
Mail-s ' SUBJECT ' [email protected] </path/from/somefile
COMMAND | Mail-s ' SUBJECT ' [email protected]
To see if the Mail Service started: Netstat-tlnup | grep 25
2, at command: Automatically perform some pre-set command actions at the specified date and point of time, belonging to a one-off scheduled task
To host a job that runs in the future time:
Support for using job queues:
The default is a queue;
Save Task: Ctrl+d
At [option] ... Time
Time:
(1) Absolute time
HH:MM,
Mmdd[cc]yy, Mm/dd/[cc]yy, dd.mm.[cc]yy or [cc]yy-mm-dd
Tomorrow
(2) Relative time
now+ #UNIT
Minute, hour, day, week
(3) Blur time
Midnight
Noon
Teatime
Common options:
-Q queue:at Job queue;
-f/path/from/somefile: Reads the job to be run from the specified file;
-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;
-D: Delete the specified job; equivalent to ATRM
Service Script Name:/ETC/INIT.D/ATD
Syntax format: at {hours: minutes} {year-month-day}
ATQ: User queries the currently set at Task List
ATRM: Delete the at task with the specified task number
Example: Application of RELATED commands
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
[[email protected] ~]
# date
2015年 09月 05日 星期六 18:39:54 CST
[[email protected] ~]
# at 18:41 2015-09-05
at>
touch
/aa
at> <EOT>
job 2 at 2015-09-05 18:41
[[email protected] ~]
# atq
2 2015-09-05 18:41 a root
[[email protected] ~]
# stat /aa
File:
"/aa"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: fd00h
/64768d
Inode: 14 Links: 1
Access: (0644
/-rw-r--r--
) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-09-05 18:41:01.325106256 +0800
Modify: 2015-09-05 18:41:01.325106256 +0800
Change: 2015-09-05 18:41:01.325106256 +0800
[[email protected] ~]
# at 23:59
at>
touch
/bb
at> <EOT>
job 3 at 2015-09-05 23:59
[[email protected] ~]
# atq
3 2015-09-05 23:59 a root
[[email protected] ~]
# atrm 3
[[email protected] ~]
# atq
Batch
The system chooses to run the specified task when the resource is more idle;
3, Crontab: According to the pre-set time period (minutes, hours, days ...) Repeatedly executes user-specified command actions, which are periodic scheduled tasks
Service Script Name:/etc/init.d/crond
The global configuration file is located at:/etc/crontab
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;
/etc/crontab
# * * * * * * user-name command to be executed
(2) User Cron Task: Submitted by a user, the default is to run as the submitter, so there is no need to specify the additional runner;
/var/spool/cron/username
# * * * * * command to be executed
crontab command:
crontab [-u user] [-l |-r |-e]
-u User: Not to manage your own cron task, but to specify the target user's cron task; Root has the ability to manage other users ' cron tasks; default management of their own;
-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;
Each field Description:
650) this.width=650; "title=" 123.jpg "alt=" wkiom1xqx5uwg-roaadwzjgmdzu927.jpg "src=" http://s3.51cto.com/wyfs02/M01 /72/ab/wkiom1xqx5uwg-roaadwzjgmdzu927.jpg "/>
Special representations of time values:
*: Indicates any time within the range
,: Multiple discontinuous points of time representing the interval
-: Represents a continuous range of time
/: Specify the time frequency of the interval
1
2
3
4
5
例:
0 17 * * 1-5 周一到周五每天17:00
30 8 8 * 1,3,5 每周一,三,五的8点30分
0 8-18
/2
* * * 8点到18点之间每个两小时
0 * *
/3
* * 每隔三天
system default settings and scripts, located in directory/etc/cron.*/
User-defined This is, located in the file/var/spool/cron/user name
Manage Cron Scheduled Tasks:
Edit Scheduled Task: crontab–e {-u user name}
View Scheduled Tasks: crontab–l {-u user name}
Delete Scheduled Task: crontab–r {-u user name}
Attention:
(1) If you do not want to receive notification messages for task execution results:
COMMAND >/dev/null
COMMAND &>/dev/null
(2) for crontab file,% has a special function, if the command will appear in the%, remember to escape, or use single quotation marks to its reference;
such as:/bin/cp-rfp/etc//backups/etc-' Date +\%y\%m\%d '
(3) Crontab's path variable is not exactly the same as the user's variable, so it is recommended that the task in cron use an absolute path
such as:/root/bin/a.sh
Homework:
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;
3 * * 1-6/bin/cp-af/etc//backups/etc-' Date +%y%m%d '
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/73/02/ Wkiom1xxkvvgevknaaazzzzogiq420.jpg "" 244 "height="/>
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;
2 * * 0/bin/cp–af/etc/fstab/backup/fstab-' date +%y%m%d%x '
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/73/02/wKiom1XxkvzBMFkuAAAmO-ja_ Og036.jpg "" 244 "height="/>
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 * * * grep-e ' ^ (s| M) '/proc/meminfo >>/statistics/meminfo.txt&& echo ==================== >>/statistics/ Meminfo.txt
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/73/02/ Wkiom1xxkvyxpfsyaaaub9pobmg692.jpg "" 244 "height="/>
Linux Scheduled Tasks