码迷,mamicode.com
首页 > 其他好文 > 详细

计划任务和周期任务mail,at,batch,atq, atrm, cron, crontab

时间:2015-09-06 11:15:26      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:mail   at   batch   atq   atrm   cron   

计划任务和周期任务mail,at,batch,atq, atrm, cron, crontab

 

Linux上的任务计划:

                任务计划:

                                在未来时间点一次性执行某任务:at,batch

                                周期性执行某任务:crontab

                电子邮件服务:

                查看邮件服务是否启动:netstat -tlnup |grep "25" //25 port  smtpservice normal

               

                默认收发邮件仅能在本机上的用户间进行,且只能通过回环接口上的地址进行

 

                                smtp:simple mail transmission protocol

                                                /var/spool/mail/USERNAME

 

                                pop3: postoffice protocol

                                imap4: internetmail access protocol

 

                                MUAmail命令

                                mail命令:

 

                                                mail:进入交互式收发邮件接口;

                                                mail-s ‘SUBJECT‘ username@hostname

                                                mail-s ‘SUBJECT‘ username@hostname < /PATH/FROM/SOMEFILE

                                                COMMAND| mail -s ‘SUBJECT‘ username@hostname

 

                at命令:

 

                                承载未来时间运行的某作业:

                                The at commandschedules a command to be run once at a particular time.

                                This can be anycommand that you normally have permission to run; anything from a simplereminder message, to a complex script.

                                You start byrunning the at command at the command line, passing it the scheduled time asthe option.

                                It then placesyou at a special prompt, where you can type in the command (or series ofcommands) to be run at the scheduled time.

                                When you‘redone, press Control-D on a new line, and your command will be placed in thequeue

 

                                支持使用作业队列:默认为a队列;

 

                                Ctrl+d 提交/确认

 

                                at [option]...TIME

 

                                TIME:

                                                (1)绝对时间

                                                                HH:MM,

                                                                MMDD[CC]YY,MM/DD/[CC]YY, DD.MM.[CC]YY or [CC]YY-MM-DD

                                                                tomorrow

 

                                                                2:30PM 21.10.14   "2:30 PM October 212014"

                                                                2:30PM 10/21/2014 "2:30 PM October 21 2014"

                                                (2)相对时间

                                                                now+#UNIT

                                                                                minute,hour, day, week

                                                                                now+ 30 minutes "10:30 AM October 18 2014"

                                                (3)模糊时间

                                                                midnight1200AMXXXXX

                                                                noon     1200PM XXXXX

                                                                teatime  "4:00 PM October 18 2014"

                                                                atnow + 10 years

 

                                                ...andthen enter a command at the at> prompt, press enter, and type Control-D, youwill be mailed the results of your command ten years from now

                                                Usingatq To View Your at Queue

                                                Thisinformation is, from left to right: job number, date, hour, year, queue, andusername.

                                                atq will only list jobs that belong toyou — unless you are the super user, in which case it will list the jobs of allusers. So to list all at jobs currently queued on the system, type this command(if you have superuser privileges):

                                                sudo atq

                                常用选项:

                                                -qQUEUEat作业队列;

                                                -f/PATH/FROM/SOMEFILE:从指定的文件中读取要运行的作业;

                                                -l:查看作业队列中的等运行作业列表;相当于使用atq命令;

                                                -cAT_JOB_NUM: 查看待运行作业的内容;

                                                -d:删除指定作业;相当于atrm

 

                                实例:

                                at 9:30 PM Tue

 

                                warning:commands will be executed using /bin/sh

                                at> echo"Well gosh golly, it‘s 9:30 PM on Tuesday."

                                at> ^D

                                job 1 at Tue Nov16 09:30:00 2014

 

                                Note: The outputof your specified command will be mailed to you. You can read this mail withthe mail program, or a program like pine (or the modern version of pine, calledalpine). You can download these programs if you don‘t have them, or installthem with your package manager; for example, on Ubuntu, which uses the APTpackage management system, you can install them using the apt-get command,specifically: sudo apt-get install mail or sudo apt-get install alpine.

 

                batch:

                                由系统自行选择在资源较为空闲时运行指定的任务;batchexecutes commands when system load levels permit; in other words, when the loadaverage drops below 1.5, or the value specified in the invocation of atd.

 

                                Files

                                                /var/spool/cron/atjobs

                                                /var/spool/cron/atspool

                                                /proc/loadavg

                                                /var/run/utmp

                                                /etc/at.allow

                                                /etc/at.deny

 

                                Examples

                                at -m 01:35 <my-at-jobs.txt

                                                Runthe commands listed in the ‘my-at-jobs.txt‘ file at 1:35 AM. All output fromthe job will be mailed to the user running the task. When this command has beensuccessfully entered you should receive a prompt similar to the example below:

 

                                                commandswill be executed using /bin/sh

                                                job1 at Wed Dec 24 00:22:00 2014

 

                                at -l

                                                Thiscommand will list each of the scheduled jobs in a format like the following:

                                                1          Wed Dec 24 00:22:00 2003...this isthe same as simply running the command atq.

 

                crontab:周期性任务计划

                                                                Thecrontab is a list of commands that you want to run on a regular schedule, andalso the name of the command used to manage that list. crontab stands for"cron table," because it uses the job scheduler cron to executetasks; cron itself is named after "chronos," the Greek word for time.

 

                                守护进程:crond

 

                                周期性任务有两类:

                                (1) 系统cron任务;没有默认运行用户身份,所以需要额外指定运行者;

                                                /etc/crontab

                                                vim命令

 

                                                                #Example of job definition:

                                                                #.---------------- minute (0 - 59)

                                                                #|  .------------- hour (0 - 23)

                                                                #|  | .---------- day of month (1 - 31)

                                                                #|  | |  .------- month (1 - 12) ORjan,feb,mar,apr ...

                                                                #|  | |  |  .---- day of week (0 - 6) (Sunday=0 or 7) ORsun,mon,tue,wed,thu,fri,sat

                                                                #|  | |  |  |

                                                                #*  * *  *  * user-name command to be executed        

 

                                                                7个字段:

                                                                                5个字段:时间点

                                                                                user-name:以哪个用户的身份运行任务

                                                                                commandto be executed: 要运行任务                                               

 

                                (2) 用户cron任务:由某个用户所提交,默认就以提交者的身份运行,所以无需额外指定运行者;

                                                /var/spool/cron/USERNAME

 

                                                vim命令;不建议

                                                crontab命令:建议

 

                                                                #Example of job definition:

                                                                #.---------------- minute (0 - 59)

                                                                #|  .------------- hour (0 - 23)

                                                                #|  | .---------- day of month (1 - 31)

                                                                #|  | |  .------- month (1 - 12) ORjan,feb,mar,apr ...

                                                                #|  | |  |  .---- day of week (0 - 6) (Sunday=0 or 7) ORsun,mon,tue,wed,thu,fri,sat

                                                                #|  | |  |  |

                                                                #*  * *  *  * command to be executed

 

                                                                5个时间点:

                                                                                分钟:有效取值范围0-59

                                                                                小时:0-23

                                                                                日:1-31

                                                                                月:1-12

                                                                                周几:0-7

 

                                                                                注意:几月几号与周几,不建议同时使用;

 

                                                                                例如:

                                                                                                6* * * *

 

 

                                                                                时间表示法:

 

                                                                                                *:对应时间点有效取值范围内的每个时间点;

                                                                                                -:一个特定连续时间范围,3-7

                                                                                                ,:一个离散的时间点,3,5,7

                                                                                                /#:有效时间范围内的每多少时间;用于指定频率;

                                                                                                                1-30/4,*/4

 

                                                                                5*/3 * * * /bin/echo "howdy"

 

                                                                                57 * * 1-5 /bin/echo "howdy"

 

                                                                crontab命令:

                                                                                crontab[-u user] [-l | -r | -e]

                                                                                           -uuser: 不是管理自己的cron任务,而是指定的目标用户的cron任务;仅root拥有管理其它用户的cron任务的权限;默认管理自己的;

 

                                                                                                -l:list,列出任务;

                                                                                                -r:remove, 移除所有任务;

                                                                                           -e: edit,编辑,打开一当前shell会话的默认编辑器来编辑cron任务表;

 

                                                                Afield may be an asterisk (*), which always stands for "first throughlast".

 

                                                                Rangesof numbers are allowed. Ranges are two numbers separated with a hyphen. Thespecified range is inclusive; for example, 8-11 for an "hours" entryspecifies execution at hours 8, 9, 10 and 11.

 

                                                                Listsare allowed. A list is a set of numbers (or ranges) separated by commas.Examples: "1,2,5,9", "0-4,8-12".

 

                                                                Stepvalues can be used in conjunction with ranges. For example, "0-23/2"can be used in the hours field to specify command execution every other hour.Steps are also permitted after an asterisk, so if you want to say "everytwo hours", you can use "*/2".

 

                                                                Namescan also be used for the "month" and "day of week" fields.Use the first three letters of the particular day or month (case doesn‘tmatter). Ranges or lists of names are not allowed.

 

                                                                The"sixth" field (the rest of the line) specifies the command to be run.The entire command portion of the line, up to a newline or % character, will beexecuted by /bin/sh or by the shell specified in the SHELL variable of thecronfile. Percent-signs (%) in the command, unless escaped with backslash (\),will be changed into newline characters, and all data after the first % will besent to the command as standard input.

 

                                                                Notethat the day of a command‘s execution can be specified by two fields: day ofmonth, and day of week. If both fields are restricted (in other words, theyaren‘t *), the command will be run when either field matches the current time.For example, "30 4 1,15 * 5" would cause a command to be run at 4:30am on the 1st and 15th of each month, plus every Friday

 

 

                                                                思考:

                                                                                1、如果想实现每8分钟运行一次某任务?*/8* * * * /bin/echo "howdy"

                                                                                2、如果想实现每10秒钟运行一次某任务?

 

                                                                                #!/bin/envbash

                                                                                while[ true ];

                                                                                do   # do what you need to here

                                                                                                sleep30

                                                                                               

                                                                                done

 

                注意:

                                                (1)如果不想接收任务执行结果的通知邮件:

                                                                COMMAND> /dev/null

                                                                COMMAND&> /dev/null

                                                (2)对于crontab文件来讲,%有特殊功用;如果命令中会出现%,要记得转义,或者用单引号对其引用;

                                                (3)crontabPATH变量与用户的变量不完全相同,所以,建议在cron中的任务要使用绝对路径

                                                                /root/bin/a.sh

                                                               

                补充性的工具:anacron:

 

                                Anacron is thecron for desktops and laptops. Anacron does not expect the system to be running24 x 7 like a server.

                                When you want abackground job to be executed automatically on a machine that is not running 24x 7, you should use anacron. For example, if you have a backup script scheduledeveryday at 11 PM as a regular cron job, and if your laptop is not up at 11 PM,your backup job will not be executed. However, if you have the same jobscheduled in anacron, you can be sure that it will be executed once the laptopcome back up. Anacrontab Format Just like how cron has /etc/crontab, anacronhas /etc/anacrontab.

                                /etc/anacrontabfile has the anacron jobs mentioned in the following format.

 

 

               

 

练习:

                1、每周一到周六的凌晨320分,运行cp命令对/etc/目录进行归档另存,存储位置为/backups/etc-YYYY-MM-DD;

                2、每周日凌晨230分,运行cp命令对/etc/fstab文件进行备份,存储位置为/backup/fstab-YYYY-MM-DD-hh-mm-ss

                3、每天晚上12点,取得/proc/meminfo文件中所有以SM开头的行,追加至/statistics/meminfo.txt文件中,且每天的消息之前,要加上类似===============分隔线;


本文出自 “IT_working” 博客,请务必保留此出处http://weine100.blog.51cto.com/2152945/1691690

计划任务和周期任务mail,at,batch,atq, atrm, cron, crontab

标签:mail   at   batch   atq   atrm   cron   

原文地址:http://weine100.blog.51cto.com/2152945/1691690

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!