09/05做業

Linux任務計劃shell

任務計劃:vim

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

                   週期性執行某任務: crontabide

atspa

 

                   承載將來時間運行的某做業:code

 

                   支持使用做業隊列:隊列

                            默認爲a隊列;進程

                   Ctrl+d保存任務crontab

                   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 QUEUE at做業隊列;

                            -f/PATH/FROM/SOMEFILE:從指定的文件中讀取要運行的做業;

                            -l: 查看做業隊列中的等運行做業列表;至關於使用atq命令;

                            -c AT_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: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

                                                        注意:幾月幾號與周幾,不建議同時使用;

                                                        時間表示法:

                                                                 *:對應時間點有效取值範圍內的每一個時間點;

                                                                 -:一個特定連續時間範圍, 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任務表;

練習

每週一到週六的凌晨320分,運行cp命令對/etc/目錄進行歸檔另存,存儲位置爲/backups/etc-YYYY-MM-DD

20 3 * * 1-6/bin/cp  /backups/etc-‘date+%d’

每週日凌晨230分,運行cp命令對/etc/fstab文件進行備份,存儲位置爲/backup/fstab-YYYY-MM-DD-hh-mm-ss

30 2 * */bin/cp  /etc/fstab-‘date+%Y-%m-%d-%H-%M-%S’

相關文章
相關標籤/搜索