簡體   English   中英

如何每周日運行crontab作業

[英]How to run crontab job every week on Sunday

我想弄清楚如何在每周日的星期日運行 crontab 作業。 我認為以下應該有效,但我不確定我是否理解正確。 以下是正確的嗎?

5 8 * * 6

這里是對 crontab 格式的解釋。

# 1. Entry: Minute when the process will be started [0-60]
# 2. Entry: Hour when the process will be started [0-23]
# 3. Entry: Day of the month when the process will be started [1-28/29/30/31]
# 4. Entry: Month of the year when the process will be started [1-12]
# 5. Entry: Weekday when the process will be started [0-6] [0 is Sunday]
#
# all x min = */x

因此,根據此,您的5 8 * * 0將在每個星期日的 8:05 運行。

要在周日執行 cron,您可以使用以下任一方法:

5 8 * * 0
5 8 * * 7
5 8 * * Sun

其中5 8代表發生這種情況的一天中的時間:8:05。

通常,如果您想在星期天執行某些操作,只需確保第 5 列包含07Sun 你有6 ,所以它在星期六運行。

cronjobs 的格式是:

 +---------------- minute (0 - 59)
 |  +------------- hour (0 - 23)
 |  |  +---------- day of month (1 - 31)
 |  |  |  +------- month (1 - 12)
 |  |  |  |  +---- day of week (0 - 6) (Sunday=0 or 7)
 |  |  |  |  |
 *  *  *  *  *  command to be executed

您始終可以使用crontab.guru作為編輯器來檢查您的 cron 表達式。

以下是 crontab 文件的格式。

{minute} {hour} {day-of-month} {month} {day-of-week} {user} {path-to-shell-script}

因此,要在每個星期日的午夜運行(星期日通常為 0,在極少數情況下為 7):

0 0 * * 0 root /path_to_command

crontab 網站給出了實時結果顯示: https : //crontab.guru/#5_8_*_*_0

在此處輸入圖片說明

在指定您的 cron 值時,您需要確保您的值在范圍內。 例如,某些 cron 使用 0-7 范圍表示星期幾,其中 0 和 7 都代表星期日。 我們不(檢查下面)。

Seconds: 0-59
Minutes: 0-59
Hours: 0-23
Day of Month: 1-31
Months: 0-11
Day of Week: 0-6

參考: https : //github.com/ncb000gt/node-cron

我想你會喜歡這個交互式網站,它經常幫助我構建復雜的 Crontab 指令: https : //crontab.guru/

10 * * * 太陽

Position 1 for minutes, allowed values are 1-60
position 2 for hours, allowed values are 1-24
position 3 for day of month ,allowed values are 1-31
position 4 for month ,allowed values are 1-12 
position 5 for day of week ,allowed values are 1-7 or and the day starts at Monday. 

以人類可讀的方式crontab 構建器的Cron 作業表達式

@weekly 更適合我! example,add the fellowing crontab -e ,it will work in every sunday 0:00 AM @weekly /root/fd/databasebackup/week.sh >> ~/test.txt

* * * * 0 

you can use above cron job to run on every week on sunday, but in addition on what time you want to run this job for that you can follow below concept :

* * * * *  Command_to_execute
- � � � -
| | | | |
| | | | +�� Day of week (0�6) (Sunday=0) or Sun, Mon, Tue,...
| | | +���- Month (1�12) or Jan, Feb,...
| | +����-� Day of month (1�31)
| +������� Hour (0�23)
+��������- Minute (0�59)

如果您不關心它在一天中的什么時間運行,我真的很想使用 @weekly 關鍵字運行。 它應該在每個星期天運行,並且絕對更具可讀性。

@weekly some_script.sh

暫無
暫無

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

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