Unix

“本月最後一天”的 Cron 表達式中的 L 字元

  • December 12, 2012

我目前正在寫一篇關於 cron 的文章,面向熟悉該工具但不一定經常使用它的人。我希望這些範例與使用中的大多數常見 Unix 桌面兼容(2002 年後的 linux、bsd、osx)。

在尋找“本月最後一天”的 cron 表達式時,我發現的大多數建議都建議使用從每月 28 日到 31 日執行的 cron 表達式與包含布爾日期的命令的組合比較。

TODAY=`date +%m` # Computes the month for today (01-12)
TOMORROW=`date --date=tomorrow +%m` # Computes the month for tomorrow (01-12)
0 0 28-31 * * [$TODAY -lt $TOMORROW] && command

這似乎是一個流行的 Unix 技巧。我最近偶然發現了這篇關於 Cron 表達式的維基百科文章,其中討論了一些我在其他任何地方都沒有提到的字元,甚至沒有手冊頁。根據文章,L 字元可以在表達式中用於指定月份(或星期)的最後一天。

哪個版本的 cron 支持這個?它的支持範圍有多廣?為什麼它如此鮮為人知?

我不知道 L 語法,但在 BSD 上,您可以使用:

      string     meaning
      ------     -------
      @reboot    Run once, at cron(8) startup.
      @yearly    Run every January 1, "0 0 1 1 *".
      @annually  (same as @yearly).
      @monthly   Run the first day of every month, "0 0 1 * *".
      @weekly    Run every Sunday, "0 0 * * 0".
      @daily     Run every midnight, "0 0 * * *".
      @midnight  (same as @daily).
      @hourly    Run every hour, on the hour, "0 * * * *".

但這也不是標準…

我從未聽說過這種L結構,它肯定不是我所知道的標準(根據手冊頁和一些經驗測試,絕對不是在 Vixie cron 中)。

我不會指望它在唯一的維基百科機器上工作,我建議使用你在問題中描述的方法,這幾乎可以在任何地方工作。

引用自:https://serverfault.com/questions/119942