309

我试图弄清楚如何在周日每周运行一个 crontab 作业。我认为以下应该有效,但我不确定我是否理解正确。以下是正确的吗?

5 8 * * 6
4

10 回答 10

544

这里是对 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 运行。

于 2013-05-23T15:28:19.397 回答
227

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

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

哪里5 8代表一天中发生这种情况的时间:8:05。

0一般来说,如果您想在周日执行某些操作,只需确保第 5 列包含7Sun。你有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 表达式。

于 2013-05-23T15:38:22.587 回答
43

以下是 crontab 文件的格式。

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

因此,在每个星期日午夜运行(星期日通常为 0,在极少数情况下为 7):

0 0 * * 0 root /path_to_command
于 2013-05-23T15:31:07.010 回答
19

crontab 网站给出实时结果显示:https ://crontab.guru/#5_8_*_*_0

在此处输入图像描述

于 2020-07-07T18:53:26.923 回答
6

在指定您的 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

于 2016-02-24T13:27:33.703 回答
4

我想你会喜欢这个交互式网站,它经常帮助我构建复杂的 Crontab 指令:https ://crontab.guru/

于 2019-11-19T08:49:10.770 回答
2

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

于 2018-11-13T07:05:42.700 回答
2

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. 
于 2017-10-13T05:26:00.717 回答
2

以人类可读的方式表达 Cron 作业crontab builder

于 2019-05-08T08:11:18.217 回答
0
* * * * 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)
于 2019-02-18T06:09:39.390 回答