Crontab 每?jī)芍軋?zhí)行一次 - - 香港硅谷
Crontab 每?jī)芍軋?zhí)行一次
??? 作者: Tsung Hao
??? 13-Apr-09
??? Linux
???? 瀏覽次數(shù): 1826
??? |? 評(píng)論 ↓
Tweet
今天被問(wèn)到一個(gè)問(wèn)題: Crontab 如何設(shè)定兩周執(zhí)行一次.
??? 問(wèn)題假設(shè): 每個(gè)月 "第 1, 3 周" 的 "星期一 早上6點(diǎn)" 要執(zhí)行 "ls /tmp" 的指令.
原本想想應(yīng)該是直接設(shè)定 "0 6 1-7,15-21 * 1" 就可以了, 結(jié)果 1-7, 15-21 和 星期一也都會(huì)跑.
??? man 5 crontab # 找到下述解釋
??????? Note: The day of a command's execution can be specified by two fields -- day of month, and day of week.?? If? both fields? are? restricted (i.e., aren't *), the command will be run when either field matches the current time.
??????? For example,
??????? ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
??????? 注: weekday 和 day 這兩欄很容易造成混淆, 假如兩欄同時(shí)都被指定時(shí), 只需滿足其中一欄就算符合.
目前想到的解法, 就是在程式判斷, 不然就是在 Crontab 設(shè)定時(shí)判斷, 找了很多資料, 還沒找到正確解法. (若有知道解法的, 請(qǐng)不吝指教.. Orz.)
解法
??? Crontab 中設(shè)定: "0 6 1-7,15-21 * * if [ `date '+\%w'` = "1" ]; then ls /tmp;fi"
??? 注1: bash 里面直接用 "if [ `date '+%w'` = "1" ]; then ls /tmp;fi" 即可, 但是在 Crontab 中, "%" 是特殊字符, 要加上跳脫字符(escape character).
??? 注2: "date '+%w'" => 用數(shù)字顯示星期幾 (0~6 = 星期天~ 星期六)
??? 注3: "ls /tmp" 換成想要執(zhí)行的指令即可.
相關(guān)解法
??? 找到解法看起來(lái)比較像, 但是看不懂是怎么設(shè)定的: Crontab Setting Ineffective
??? 上述采用這類的解法: biweekly schedule through cron - dBforums
相關(guān)網(wǎng)頁(yè)
??? Cron 詳細(xì)寫法: Crontab 的寫法(@reboot, @yearly...)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
