Linux Task Scheduler, recurring task execution
Perform one task at a time in the future: at, Batch
To run a task periodically: cron
e-Mail Service:
Smtp:simple Mail Transmission protocol for sending mail;
Pop3:post Office Protocol
Imap4:internet Mail Access Protocol
Mailx-send and receive Internet mail
Mua:mail User Agent
MAILX [-S ' SUBJECT '] username[@hostname]
The message body is generated:
(1) give directly, Ctrl+d;
(2) input redirection;
(3) through the pipeline;
Echo-e "How is you?\nhow old is you?" | Mail
Mailx
[Email protected] ~]# mail-s ' ssss ' [email protected]
EOT
Null message body; Hope that ' s OK
[[Email protected] ~]# mail
AT command:
At [option] Time
Time:
hh:mm [YYYY-MM-DD]
Noon, midnight, teatime
Tomorrow
Now+#{minutes,hours,days, or weeks}
Common options:
-Q QUEUE:
-L: Lists jobs waiting to be run in the specified queue; equivalent to ATQ
-D: Deletes the specified job; equivalent to ATRM
-C: View specific job tasks;
-f/path/from/somefile: Reads the task from the specified file;
Note: The execution results of the job are notified to the relevant users by email;
[[email protected] ~]# at 20:44
At> Ls/var
At> Cat/etc/fstab
At> <EOT>
Job 1 at 2016-08-10 20:44
Ctrl+d
[Email protected] ~]# at-l
You have mail in/var/spool/mail/root
[Email protected] ~]#
[Email protected] ~]# ATQ
Batch command:
Allow the system to choose its own idle time to perform the tasks specified here;
Recurring Task Scheduler: Cron
Related packages:
Cronie: The main package, provides the Crond daemon and related auxiliary tools;
Cronie-anacron:cronie to monitor the execution of Cronie tasks, such as when a task in Cronie has not run correctly in the past, the Anacron will then initiate this task;
Crontabs: Includes centos to provide system maintenance tasks;
Make sure that the Crond daemon is in a running state:
CentOS 7:
Systemctl Status Crond
..... running ...
CentOS 6:
Service Crond Status
The tasks that are scheduled to be performed periodically are submitted to Crond, which is implemented to the point of operation.
System Cron Task: system maintenance Job
/etc/crontab
User Cron Task:
crontab command
[Email protected] ~]# Rpm-qa |grep cron
Crontabs-1.10-33.el6.noarch
Cronie-1.4.4-12.el6.x86_64
Cronie-anacron-1.4.4-12.el6.x86_64
[Email protected] ~]# Rpm-qi Cronie
[[Email protected] ~]# service Crond status
Crond (PID) is running ...
System Cron Task
# Example of Job definition:
#.----------------Minute (0-59)
# | .-------------Hour (0-23)
# | | .----------Day of Month (1-31)
# | | | .-------month (1-12) OR jan,feb,mar,apr ...
# | | | | .----Day of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * * user-name command to be executed
Example: Run the echo command at 9:10 in the evening;
* * * * gentoo/bin/echo "howdy!"
Time notation:
(1) a specific value;
A value in the range of valid values for a given point in time;
(2) *
All values in the range of valid values at a given point in time;
means "every ...";
(3) Discrete value:,
#,#,#
(4) Continuous value:-
#-#
(5) in the specified time range, define the step size:
/#: #即为步长
Example: Echo command every 3 hours;
0 */3 * * * gentoo/bin/echo "howdy!"
User cron:
crontab command definition, each user has a dedicated cron task file:/var/spool/cron/username
crontab command:
crontab [-u user] [-l |-r |-e] [-I.]
-L: Lists all tasks;
-E: Editing tasks;
-R: Remove All Tasks;
-I: Used in conjunction with-R to allow users to selectively remove assigned tasks in interactive mode;
-U User: Only root can run, on behalf of the designated users to manage cron tasks;
Note: The results of the operation are notified to the relevant users by email;
(1) COMMAND >/dev/null
(2) COMMAND &>/dev/null
For cron tasks,% has a special purpose, and if you want to use% in a command, you need to escape, but if you put% in single quotes, you can not escape;
Thinking:
(1) How do I run a task at the second level?
* * * * * for min in 0 1 2; Do echo "HI"; Sleep 20; Done
(2) How do I run a task every 7 minutes?
Sleep command:
Sleep Number[suffix] ...
SUFFIX:
S: seconds, default
M: minutes
H: Hours
D: Day
Practice:
1, every 4 hours to back up once/etc directory to/backup directory, save the file name format is "ETC-YYYY-MM-DD-HH.TAR.XZ";
2, weekly 2, 4, 7 backup/var/log/messages file to/logs directory, file name is like "MESSAGES-YYYYMMDD";
3, every two hours to remove the current system in the/proc/meminfo file with s or m beginning information appended to the/tmp/meminfo.txt file;
4, weekday time, every small implementation of "IP addr Show" command;
[Email protected] ~]# Cat/etc/crontab
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/
# for details see Mans 4 Crontabs
# Example of Job definition:
#.----------------Minute (0-59)
# | .-------------Hour (0-23)
# | | .----------Day of Month (1-31)
# | | | .-------month (1-12) OR jan,feb,mar,apr ...
# | | | | .----Day of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * * user-name command to be executed
This article is from the "Liang blog" blog, make sure to keep this source http://7038006.blog.51cto.com/7028006/1837076
Linux at Batch crontab command