【问题标题】:How to run crontab job every week on Sunday如何在周日每周运行 crontab 作业
【发布时间】:2013-05-19 01:10:51
【问题描述】:

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

5 8 * * 6

【问题讨论】:

  • 问题是关于“星期天”,但接受的答案是关于“星期六”。 ¿?
  • 使用 https://crontab.guru/ 站点创建任何 cron 表达式。

标签: linux crontab


【解决方案1】:

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

【讨论】:

  • 为了更具可读性,您可以在当天使用sun, mon, tue, wed, thu, fri, or sat 之一。这也使您不必在周日使用07 之间进行选择。
【解决方案2】:

以下是 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”命令进行编辑时,需要省略用户列。)
  • 星期天午夜是星期一的零时,即0 0 * * 1
  • @FredLoney 感谢您指出这一点。 “天文学家和军方使用午夜为 0 小时的系统。在该系统中,今晚的午夜是明天的第一刻。但至于我们其他人 - 没有官方答案。这就是为什么航空公司总是将航班安排在 11 点:下午 59 点或凌晨 12:01——绝不是午夜。”通过earthsky.org/space/edit-time
【解决方案3】:

要在周日执行 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 表达式。

【讨论】:

  • 只是为了帮助其他人避免我刚刚犯下的愚蠢错误,并确保您将分钟设置为 * 以外的其他值,否则它将在该小时的每一分钟执行!
【解决方案4】:

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

【讨论】:

  • “我们”……谁?哪个程序和版本?
  • 它直接来自链接:D
【解决方案5】:

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. 

【讨论】:

  • 恭喜您在 StackOverflow 上获得第一个答案!请务必查看Answering Guide。例如,答案通常应该包含现有答案中缺少的一些新信息。
  • 这将在星期日运行 24 次,每小时 10 分钟。
【解决方案6】:

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

【讨论】:

    【解决方案7】:
    * * * * 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)
    

    【讨论】:

      【解决方案8】:

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

      【讨论】:

      • 恕我直言,这不是一个答案,因为它没有在这里添加任何内容,也没有以任何方式回答这里的问题。
      【解决方案9】:

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

      【讨论】:

        【解决方案10】:

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

        【讨论】:

          猜你喜欢
          • 2020-07-11
          • 2015-09-24
          • 2017-10-31
          • 1970-01-01
          • 2021-06-25
          • 2023-02-09
          • 1970-01-01
          • 1970-01-01
          • 2012-12-16
          相关资源
          最近更新 更多