系統的循環執行工作排程

  1. 直接以 vi 編輯檔案 /etc/crontab。
    [root@dywOffice dywang]# vi /etc/crontab
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    HOME=/
    
    01  * * * * root    run-parts /etc/cron.hourly   %*<==每小時執行的工作 *)
    02  4 * * * root    run-parts /etc/cron.daily   %*<==每天執行的工作 *)
    22  4 * * 0 root    run-parts /etc/cron.weekly  %*<==每星期執行的工作 *)
    42  4 1 * * root    run-parts /etc/cron.monthly  %*<==每個月執行的工作 *)
    分 時日月週 使用者    參數     指令
    
    ## 重新啟動crond服務
    [root@dywOffice dywang]# /etc/rc.d/init.d/crond restart
    
  2. MAILTO=root:發生錯誤時將訊息傳給 root,可將 e-mail 改成自己的帳號。例如:MAILTO=dywang@mail.cyut.edu.tw
  3. PATH= :執行檔搜尋的路徑。
  4. 01 * * * * root run-parts /etc/cron.hourly:每個小時的 01 分,系統會以 root 層級到目錄 /etc/cron.hourly 下執行所有可以執行檔。
  5. /etc/crontab 支援之指令下達方式:
    1. 指令型態:01 * * * * test mail -s test test < /home/test/test.txt
      使用者 test,每個小時執行一次指令 mail,將檔案 /home/test/test.txt 寄給 test,而抬頭為 test。
    2. 目錄規劃:*/5 * * * * root run-parts /root/runcron
      建立一個 /root/runcron 的目錄,將要每隔五分鐘執行的『可執行檔』都寫到該目錄下。

練習題

  1. 要更改 Linux 系統的系統例行性排程,可以直接以 vi 編輯那個檔案?
    Sol. /etc/crontab
  2. 更改完 Linux 系統的系統例行性排程後,如何重新啟動 crond 服務?
    Sol. /etc/rc.d/init.d/crond restart
  3. Linux 系統有一例行性排程如下,請說明其動作。
    30 * * * * root run-parts /etc/cron.hourly
    Sol. 每小時 30 分時,系統會以 root 層級的使用者去 /etc/cron.hourly 目錄下執行所有可以執行的檔案
  4. Linux 系統有一例行性排程如下,請說明其動作。
    15 8 * * * root run-parts /etc/cron.daily
    Sol. 每天 08:15,系統會以 root 層級的使用者去 /etc/cron.daily 目錄下執行所有可以執行的檔案
  5. Linux 系統有一例行性排程如下,請說明其動作。
    22 4 * * 0 root run-parts /etc/cron.weekly
    Sol. 每星期日 04:22,系統會以 root 層級的使用者去 /etc/cron.weekly 目錄下執行所有可以執行的檔案
  6. Linux 系統有一例行性排程如下,請說明其動作。
    12 5 3 * * root run-parts /etc/cron.monthly
    Sol. 每月 3 日 05:12,系統會以 root 層級的使用者去 /etc/cron.monthly 目錄下執行所有可以執行的檔案