Introduction
Perform a task at some point in the future: at, Batch
To run a task periodically: crontab
Execution result: will be sent to the user by mail
Check if the mailbox service is on: (25 port)
~]# NETSTAT-TNLP
~]# SS-TNL
Local Email service:
SMTP:SIMPLE&NBSP;MAIL&NBSP;TRANSMISSION&NBSP;PROTOCOL&NBSP;&NBSP: Simple Mail Transfer Protocol, e-mail;pop3:post office Procotol: Post Office Protocol Imap4:internet mail access procotol: Internet Mail Access Protocol Mail command: &NBSP;&NBSP;&NBSP;&NBSP;MAILX - send and receive Internet mail MUA:Mail User Agent, the user to send and receive mail the tool program; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;MAILX [-s ' SUBJECT '] username[@hostname] title user message body generation: (1) Interactive input ; point number (.) A separate row may indicate the end of the body; Ctrl+d submit also; e-Mail [[email protected] ~]# mail -s ' Hello hdfs ' hdfs How are you these days? . EOT [[email protected] ~]# Receive email mail # #输入数字查看对应第几封邮件 q #q退出 (2) via input redirect; mail -s ' Hello hdfs ' Hdfs < file.txt (body content) (3) through pipe; cat file.txt | mail -s ' Hello hdfs ' hdfs
At command: Perform a scheduled task, corresponding to the service ATD
at [option]... time time: hh:mm [yyyy-mm-dd] noon,midnight, teatime noon, at night, Tea time (four o'clock in the afternoon) tomorrow tomorrow now+# unit:minutes, hours, days, or weeks at jobs have queues, denoted by a single letter, and by default use the A queue; common options: -l: View Job queue, equivalent to atq -f /path/from/somefile: Reads the job task from the specified file without having to interactively enter the; at -f file now+5min &NBSP;-D: Deletes the specified job, which is equivalent to atrm; -c: View the specific contents of the specified job; -q&nBsp Queue: Indicates queues; note: Job execution results are sent by mail to the user submitting the job; [[ email protected] ~]# at 10:31 at> Echo qq > /root/test.log at> echo aa >> /root/test.log at> <eot > job 4 at 2017-08-25 10:31 [[email protected] ~]# at -l 4 2017-08-25 10:31 a root [[email protected] ~]# ll Total dosage 44 -rw-------. 1 root root 1223 5 Month &NBSP;&NBsp;18 15:10 anaconda-ks.cfg -rw-r--r--. 1 Root root 25166 5 Month 18 15:10 install.log -rw-r--r--. 1 root root 7992 5 Month 18 15:09 install.log.syslog [[email protected] ~]# At -l 4 2017-08-25 10:31 a root [[email protected] ~]# at -l [[email protected] ~]# ll Total dosage 48 -rw-------. 1 root root 1223 5 Month 18 15:10 anaconda-ks.cfg -rw-r--r--. 1 root root 25166 5 Month 18 15:10 install.log -rw-r--r--. 1 root root 7992 5 Month 18 15:09 install.log.syslog -rw-r--r-- 1 root root 6 8 Month 25 10:31 test.log [[email protected] ~]# cat test.log qq aa [[email protected] ~]#
Batch command:
Batch gives the system the option to perform the specified task at a time when the system resources are more idle;
Recurring Task Scheduler: Cron
Service Program: cronie: Main package, provide Crond daemon and related auxiliary tools; ensure Crond daemon (daemon) is running: CentOS 7: systemctl status crond.service active: active (running) ... ... CentOS 6: service crond status ... is running. Submitting a job to Crond is different from at, it requires a dedicated configuration file with a fixed format, it is not recommended to edit the file directly using a text editor; To use the crontab command; cron task is divided into two kinds of: system cron task: Mainly used to realize the maintenance of the system itself; Manual editing:/etc/crontab file User Cron Task: command: crontab command system cron Configuration format:/etc/crontab shell=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # example of job DEFINITION:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;#&NBSP---------------- minute (0 - 59) # | ------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | &NBSP;&NBSP;|&NBSP;&NBSP;|&NBSP;&NBSP:------- 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 Note: (1) Each row defines a recurring task with a total of 7 fields; * * * * * : Define recurring Time user-name : user identities for running tasks command to be executed: Task Command (2) The environment variable here differs from the environment that is obtained after the user logs on, so it is recommended that the command use an absolute path, or a custom path environment variable; (3) Execution result message sent to mailto named user user cron configuration format:/var/spool/cron/username shell=/ bin/bash path=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For Details see man 4 crontabs # example of job definition: # .---------------- minute (0 - 59) # | .----- -------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | &NBSP;|&NBSP;&NBSP;|&NBSP;&NBSP:------- 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 # | | | | | # * * * * * command to be executed Note: (1) Each row defines a cronServices, total 6 fields; (2) The environment variables here are different from the environment that the user has been logged into. Therefore, it is recommended that the command use an absolute path, or a custom PATH environment variable; (3) Message sent to current user; time notation: (1) Specific value; value in the range of valid values for a given point in time; Note: Day of week and Day of month is not used in conjunction with the Sun and moon. Monday use; (2) * all values in the range of valid values at a given point in time; table "per.." (3) Discrete value:, use a comma-separated number of values at a point in time; #,#,# (4) Continuous value:- Use at point-in-time-connect start and end #-# (5) Define step: at a specified point in time /#: #即步长; Example:*/# Note: (1) The specified point in time cannot be divisible by stride length, and its meaning will not exist; (2) Minimum time unit is "minute", want to complete "second" level task, need to use additional mechanism; Defined as a per-minute task: Then use the script implementation to loop through the; sample multiple times per minute: (1) 3 * * * *:performed once per hour; 3rd minutes per hour; (2) 3 4 * * 5: Once a week ; 5 of the Week 4:3; (3) 5 6 7 * *: executed once a month ; 7th per month, 6:5; (4) 7 8 9 10 *: Executed once a year ; every October 9 8:7; (5) 9 8 * * 3,7: every Wednesday and Sunday; (6) 0 8,20 * * 3,7: (7) 0 9-18 * * 1-5: (8) */5 * * * *: Perform a task once every 5 minutes; (9) */7 crontab command: crontab [-u user] [-l | -r | -e] [-i] &NBSP;&NBSP;-E: Editing tasks; -l: List All Tasks; -r: Remove all Tasks; Delete/var/spool/cron/username files; -i: Prompt user to confirm; when removing all tasks with the-r option -u user:root users can manage cron tasks for specified users; crontab -u USERNAME -e Note: The result of the operation is notified to the current user by mail, if you refuse to receive mail: (1) COMMAND > /dev/null (2) COMMAND &> /dev/null Note: When you define a command, if you need to use%, It needs to be escaped, but the percent placed in single quotes does not escape; a think: A task is not executed at the specified time due to shutdown, the next opportunity will not be automatically executed? will not!. if you expect a certain time to fail to execute, after the next boot, whether or not to the corresponding point in time to perform once, can use Anacron Realization; Homework: Anacron and its application;
Practice:
1, every 12 hours to backup the/etc directory to the/backups directory, save file name format "ETC-YYYY-MM-DD-HH.TAR.XZ" 2, weekly 2, 4, 7 backup/var/log/secure files to/logs directory, the file name format is " SECURE-YYYYMMDD "; 3. Remove the line information in the current system/proc/meminfo file starting with S or M in the/tmp/meminfo.txt file every two hours;
This article is from the "Caterpillar Small Smelly Smelly" blog, please make sure to keep this source http://moerjinrong.blog.51cto.com/11124564/1961859
Linux Task Scheduler, recurring task execution