循環執行的工作排程


		crontab		循環執行的工作排程。

  1. crontab 的使用限制如下:
    1. 先在 /etc/crontab.allow 檔案中尋找可以使用 crontab 指令的使用者。
    2. 若無 /etc/crontab.allow 檔案,則尋找 /etc/crontab.deny 檔案不能使用 crontab 指令的使用者。
    3. 若上述兩檔案皆不存在,則只有 root 可以使用 crontab 指令。
      [root@localhost csie]# cat /etc/cron.allow
      mail
      [root@localhost csie]# cat /etc/cron.deny
      cat: /etc/cron.deny: No such file or directory
      

  2. crontab:循環執行的工作排程。
    [test @test test]# crontab [-u user] [-l | -e | -r] 
    參數說明: 
    -u user :只有 root 能下達的參數,視察或編譯其他使用者的 crontab 內容 
    -l      :列出 crontab 的內容 
    -e      :編輯 crontab 的內容 
    -r      :刪除 crontab 的內容
    
  3. 編輯排程
    [root@dywOffice dywang]# crontab -e
    # min(0-59)  hours(0-23) day(1-31) month(1-12) dow(0-6)   command
    0 0 * * 6 /backup/sysbackup
    
    1. 五個時間數字參數分別代表:
      數字代表的意義 分鐘 小時 日期 月份
      範圍 0-59 0-23 1-31 1-12 0-6 (0或7為星期天)
    2. 輔助字符:
      特殊字符 代表意義
      * 代表任何時刻都接受。
      ' 代表分隔時段。
        例如:在 0300 及 0600 時工作,則 0 3,6 * * * command
      - 代表一段時間範圍內。
        例如:在 0800 到 1200 之間每小時的 20 分都進行工作,
        20 8-12 * * * command
      /n 每隔 n 單位間隔。
        例如每 5 分鐘進行一次,則 */5 * * * * command

  4. 查看排程
    [root@dywOffice dywang]# crontab -l
    # min(0-59)  hours(0-23) day(1-31) month(1-12) dow(0-6)   command
    0 0 * * 6 /backup/sysbackup
    
  5. 刪除排除
    [root@dywOffice dywang]# crontab -r
    [root@dywOffice dywang]# crontab -l
    no crontab for root
    
  6. 例題:如果您 (帳號 csie) 有個例行性程式 /home/csie/crontest,想要在每個星期一的 01:30 執行,該如何做?
    ##以root權限將使用者 csie 加入/etc/cron.allow 
    [root@localhost csie]# vi /etc/cron.allow 
    mail
    csie
    ##以使用者 csie 帳號編輯 crontab
    [csie@localhost csie]$ crontab -e
    ###或 root 直接幫 csie 編輯 crontab
    [root@localhost csie]# crontab -u csie -e
    
    # min(0-59)  hours(0-23) day(1-31) month(1-12) dow(0-6)   command
    30 1 * * 1 /home/csie/crontest
    
    ## 重新啟動 crond 服務
    [root@localhost csie]# /etc/rc.d/init.d/crond restart
    

練習題

  1. 請說明工作排程指令 crontab 之限制使用者原理。
    Sol. 先找尋 /etc/crontab.allow,寫在此檔案的使用者才能使用;若 /etc/crontab.allow 不存在,就尋找 /etc/crontab.deny ,寫在 crontab.deny 的使用者則不能使用;若兩個檔案都不存在,只有 root 可以使用 crontab 指令。
  2. 請寫出在每個星期一的 01:30 執行 cmd 的例行性排程。
    Sol. 30 1 * * 1 cmd
  3. 請寫出在 9 月 15 日,每 5 分鐘 cmd 的例行性排程。
    Sol. */5 * 15 9 * cmd
  4. 請寫出在 0800 到 1200 之間每小時的 20 分都進行 cmd 的例行性排程。
    Sol. 20 8-12 * * * cmd
  5. 請寫出在每個月 10 日 0300 及 0600 時工作進行 cmd 的例行性排程。
    Sol. 0 3,6 10 * * cmd
  6. Linux 系統有一例行性排程如下,請說明其動作。
    03 02 * * * cmd
    Sol. 每天 02:03 執行命令 cmd
  7. Linux 系統有一例行性排程如下,請說明其動作。
    0 12 2 * * cmd
    Sol. 每月 2 日 12:00 執行命令 cmd
  8. Linux 系統有一例行性排程如下,請說明其動作。
    03 14 * 5 * cmd
    Sol. 5 月,每天 14:03 執行命令 cmd
  9. Linux 系統有一例行性排程如下,請說明其動作。
    03 02 * * 0 cmd
    Sol. 每星期天 02:03 執行命令 cmd
  10. Linux 系統有一例行性排程如下,請說明其動作。
    0 0 1 1 * cmd
    Sol. 每年一月一日 00:00 執行命令 cmd
  11. Linux 系統有一例行性排程如下,請說明其動作。
    */3 * * * * cmd
    Sol. 每 3 分鐘執行命令 cmd
  12. Linux 系統有一例行性排程如下,請說明其動作。
    * */12 * * * cmd
    Sol. 每 12 小時執行命令 cmd
  13. Linux 系統有一例行性排程如下,請說明其動作。
    03 14 1,15 * * cmd
    Sol. 每月一日及十五日, 14:03 執行命令 cmd
  14. Linux 系統有一例行性排程如下,請說明其動作。
    03 02 * * 0,6 cmd
    Sol. 每星期天及星期六 02:03 執行命令 cmd
  15. Linux 系統有一例行性排程如下,請說明其動作。
    0 0 * * 1-5 cmd
    Sol. 每星期一至星期五 00:00 執行命令 cmd
  16. Linux 系統有一例行性排程如下,請說明其動作。
    0 9-12 * * 3 cmd
    Sol. 每星期三 9:00 10:00 11:00 12:00 執行命令 cmd
  17. 系統管理者 root 要編輯帳號 csie 的例行性排程,如何下指令?
    Sol. crontab -u csie -e
  18. 系統管理者 root 要列出帳號 csie 的例行性排程,如何下指令?
    Sol. crontab -u csie -l
  19. 系統管理者 root 要刪除帳號 csie 的例行性排程,如何下指令?
    Sol. crontab -u csie -r
  20. 編輯自己的例行性排程,如何下指令?
    Sol. crontab -e
  21. 列出自己的例行性排程,如何下指令?
    Sol. crontab -l
  22. 刪除自己的例行性排程,如何下指令?
    Sol. crontab -r

  DYWANG_HOME