Product SiteDocumentation Site

9.7. 以 cronatd 使用排定的工作

cron is the daemon responsible for executing scheduled and recurring commands (every hour, every day, every week, etc.). atd deals with commands to be executed a single time, but at a specific moment in the future.
在 Unix 系統裡,很多工作都需定期規律地執行:
預設所有的使用者都可定期執行工作。每個使用者有自己的 crontab 記錄定期執行的命令。可以使用 crontab -e (其內容儲存在 /var/spool/cron/crontabs/user 檔案內) 命令編輯它。
根使用者有自己的 crontab,不過也使用 /etc/crontab 檔案,或寫入額外的 crontab 檔案於 /etc/cron.d 資料夾內。最後兩個解決方案有其優勢,在執行命令時辦識使用者。
cron 套件包括預設定期執行的命令:
很多 Debian 套件依賴此服務:把維護腳本置於此資料夾,確保其服務的最佳運作。

9.7.1. crontab 檔案的格式

Each significant line of a crontab entry describes a scheduled command with the six (or seven) following fields:
  • the value for the minute (from 0 to 59);
  • 小時值 (從 0 到 23);
  • 每月的日數值 (從 1 到 31);
  • 月的值 (從 1 到 12);
  • 每週的日數值 (從 0 到 7,1 表示星期一,星期天可以是 0 或 7;也可用星期的前三個英文字母表示,如 SunMon等);
  • 必須以使用者名稱執行 (在 /etc/crontab 檔案與其位在 /etc/cron.d/ 內,但不在使用者自己的 crontab 檔案內);
  • 執行的命令 (滿足前五個欄位的條件時)。
這些內容的詳情記錄在 crontab(5) 手冊內。
Each value can also be expressed in the form of a list of possible values (separated by commas). The syntax a-b describes the interval of all the values between a and b. The syntax a-b/c describes the interval with an increment of c (example: 0-10/2 means 0,2,4,6,8,10). An asterisk * is a wildcard, representing all possible values.

範例 9.2. Sample user crontab file

#Format
#min hour day mon dow  command

# Download data every night at 7:25 pm
 25  19   *   *   *    $HOME/bin/get.pl

# 8:00 am, on weekdays (Monday through Friday)
 00  08   *   *   1-5  $HOME/bin/dosomething

# every two hours
 *  */2   *   *   *    $HOME/bin/dosomethingelse

# Restart the IRC proxy after each reboot
@reboot /usr/bin/dircproxy

9.7.2. 使用 at 命令

The at executes a command at a specified moment in the future. It takes the desired time and date as command-line parameters, and the command to be executed in its standard input. The command will be executed as if it had been entered in the current shell. at even takes care to retain the current environment, in order to reproduce the same conditions when it executes the command. The time is indicated by following the usual conventions: 16:12 or 4:12pm represents 4:12 pm. The date can be specified in several European and Western formats, including DD.MM.YY (27.07.22 thus representing 27 July 2022), YYYY-MM-DD (this same date being expressed as 2022-07-27), MM/DD/[CC]YY (i.e., 12/25/22 or 12/25/2022 will be December 25, 2022), or simple MMDD[CC]YY (so that 122522 or 12252022 will, likewise, represent December 25, 2022). Without it, the command will be executed as soon as the clock reaches the time indicated (the same day, or tomorrow if that time has already passed on the same day). You can also simply write “today” or “tomorrow”, which is self-explanatory.
$ at 09:00 27.07.22 <<END
> echo "Don't forget to wish a Happy Birthday to Raphaël!" \
>   | mail lolando@debian.org
> END
warning: commands will be executed using /bin/sh
job 1 at Wed Jul 27 09:00:00 2022
還有另個方法可以延後執行命令的時間點:at now + 數字 間隔間隔 可以是 小時、或 數字 指明執行命令前的時間。
取消 cron 排定的工作,祗需執行 crontab -e 並刪除在 crontab 檔案中對應的列。對於使用 at 的工作,同樣簡單:執行 atrm 工作編號 就可以了。排定時由 at 命令指定工作編號,可以用 atq 命令列出當前工作的清單,籨而找到該工作編號。