Cron 每第二个星期三运行一次?

11 linux cron

我需要找到一种方法来安排工作,以便它在每个月的第二个星期三运行。这可能吗?

Has*_*kun 18

我的 crontab 联机帮助页(很遗憾我在网上找不到)给出了以下示例:

# Run on every second Saturday of the month
0 4 8-14 * *    test $(date +\%u) -eq 6 && echo "2nd Saturday"
Run Code Online (Sandbox Code Playgroud)

将其调整为您的目的...

0 4 8-14 * *    test $(date +\%u) -eq 3 && job.sh
Run Code Online (Sandbox Code Playgroud)

  • 对于希望编辑此答案的任何人,您不只使用星期几字段的原因是,如果月份中的日期和星期几都受到限制(即不是“*”),则该命令将在以下时间运行_任何一个_匹配。 (6认同)

小智 5

对于 CentOS 7 服务器,这似乎是适合我的语法。[请注意和周围的空格]。我花了一段时间才弄清楚。

这将test.sh在该月第二个星期三的下午 13:07 / 1:07 运行该文件。(0=星期日、1=星期一、2=星期二、3=星期三等)

07 13 8-14 * * [ `date +\%u` = 3 ] &&  /root/scripts/test.sh
Run Code Online (Sandbox Code Playgroud)


edo*_*lin 0

单独使用 cron 是不可能的,但您可以每周调用一次脚本来进行测试:

\n

在 crontab 中,每周三中午 12 点运行 secondary_wed.sh:

\n
0 12 * * 3 /home/you/bin/second_wed.sh\n
Run Code Online (Sandbox Code Playgroud)\n

在 Second_wed.sh 中:

\n
#!/usr/bin/env bash\n\n#get day of month\nday_of_month=`date +%d`\n\n#if this day is between 7th and 15th day of the month = 2nd week\nif [ $day_of_month -gt 7 -a $day_of_month -lt 15 ]; then\n  # Call your program here instead of 'ls'\xe2\x80\xa6\n  ls\nfi\n
Run Code Online (Sandbox Code Playgroud)\n