計劃任務和週期任務mail,at,batch,atq, atrm, cron, crontab

Linux上的任務計劃:           shell

            任務計劃:vim

                        在將來時間點一次性執行某任務:at,batch編輯器

                        週期性執行某任務:crontab ide

            電子郵件服務:工具

                        smtp: simple mailtransmission protocolpost

                                    /var/spool/mail/USERNAMEspa

                        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

相關文章
相關標籤/搜索