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

Linux上的任务计划:           

            任务计划:

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

                        周期性执行某任务:crontab 

            电子邮件服务:

                        smtp: simple mailtransmission protocol

                                    /var/spool/mail/USERNAME

                        pop3: post officeprotocol

                        imap4: internet mailaccess protocol

                        MUAmail命令

                        mail命令:

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

                                    mail -s'SUBJECT' username@hostname

 

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

                                    COMMAND |mail -s 'SUBJECT' username@hostname 

            at  命令: 

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

                        支持使用作业队列:

                                    默认为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) 相对时间

                                                now+#UNIT

                                                            minute,hour, day, week

                                    (3) 模糊时间

                                                midnight

                                                noon

                                                teatime 

                        常用选项:

                                    -q QUEUEat作业队列;

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

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

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

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

            batch:

                        由系统自行选择在资源较为空闲时运行指定的任务;

 

            crontab:周期性任务计划

                        守护进程: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任务表; 

                                                思考:

                                                            1、如果想实现每8分钟运行一次某任务?

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

                                    补充性的工具:anacron:

                       注意:

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

                                                            COMMAND> /dev/null

                                                            COMMAND&> /dev/null

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

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

                                                            /root/bin/a.sh


你可能感兴趣的:(cron,mail,batch,at,atq)