簡體   English   中英

從上午 9 點 21 分到下午 2 點 30 分每 2 分鍾安排一次 cron 作業

[英]Schedule cron job from 9:21AM till 02:30PM every 2 minutes

我想安排一個cron job從周一到周五每 2 分鍾從上午 9:21 到下午 02:30 運行。 我怎樣才能做到這一點?。 我知道以下可以從上午 9 點到下午 4 點執行此操作,我該如何修改它以實現上述條件。

提前致謝。

*/2 09-16 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log

這實際上是正確 cron 表達式的開始。 為了得到你正在尋找的東西,你實際上必須結合幾個預定的表達式,我相信

我強烈推薦使用工具Crontab Guru來檢查你的 crontab 表達式!

# “At every 2nd minute from 21 through 60 past hour 9 on every day-of-week from Monday through Friday.”
21-60/2 9 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log

# At every 2nd minute past every hour from 10 AM through 1:59 PM on every day-of-week from Monday through Friday.
*/2 10-13 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log

# At every 2nd minute from 0 through 30 past hour 14 on every day-of-week from Monday through Friday.
0-30/2 14 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log



另一個可能有用的注意事項是檢查 cron 郵件(我看到您正在記錄,這很棒。)但系統通常也會提供詳細說明 cron 作業的 cron 郵件。

通常可以在 /var/spool/mail/{username} 中找到

手冊頁 ( man 5 crontab ) 中沒有指示任何單行規范都支持您需要的內容,因為您設置的任何范圍都將分別應用於每個時間字段(例如分鍾、小時),例如21-30/2 9-14...意味着在每個小時之后的 21、23、25、27、29 分鍾運行。

您當然可以使用多行來達到預期的效果:

21-59/2 9     * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
1-59/2  10-13 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
1-30/2  14    * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log

在這種情況下,間隔是一個小時的因子這一事實有所幫助,因此至少這三行的中間將確保在 10:01 - 13:59 期間有規律的間隔。 如果您有 7 分鍾的間隔,那么您將需要更多的行來確保始終有一個完全規則的間隔。

另請注意手冊頁中的以下注釋:

crontab 語法無法定義所有可能的時期,人們可以想象出來。 例如,定義一個月的最后一個工作日並不簡單。 如果任務需要在 crontab 語法中無法定義的特定時間段內運行,最好的方法是讓程序本身檢查日期和時間信息,並僅在時間段與所需時間段匹配時繼續執行。

因此,您可以采用這種方法並僅使用例如:

1-59/2 9-14     * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log

並在您的 shell 腳本(或者可能是包裝器腳本)中執行一些測試,例如:

hhmm=`date +%H%M`
if [ $hhmm -lt 0930 -o $hhmm -gt 1430 ]; then exit; fi

(這里我們將hhmm視為 4 位十進制數)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM