0

Is there any way to schedule execution of CRON:

  • only during weeks 25 - 36 during the year
  • and only on Mon - Frid (not weekends)

This is because of summer holidays when people place a lot of orders on our products. It is pretty tedious to change CRON settings every week. I understand that @weekly cannot be combined with star-settings, so something like following is not valid markup:

* * 1,2,3,4,5 * * @weekly php /app/cmd import >> /logs/cron-offers.log
lubosdz
  • 145
  • 1
  • 1
  • 5

2 Answers2

2

You should be able to do this in this way:

# m h  dom mon dow   command
* * * * 1-5 /bin/bash -c "if [ `date +%W` -lt 15 ] && [ `date +%W` -gt 11 ]; then php /app/cmd import >> /logs/cron-offers.log; fi"

Put your correct number of week in the first and second "[]".

Thomas
  • 4,155
  • 5
  • 21
  • 28
mariaczi
  • 236
  • 1
  • 5
  • Wow, thank you for your answer, it seems to work. Though I will need to expand the condition a little, because holidays are in weeks 17-18, 25-36, 39 and during Christmas weeks 51-52 :-o) – lubosdz Mar 15 '18 at 11:59
1

Cron is all things considered a very basic scheduler and the syntax does not easily allow an administrator to formulate slightly more uncommon schedules. Stock cron does not have a weeknumber available, so you have to be creative.

According to ncal -w 2018 weeks 25-36 is:

    May               June              July              August          September
Mo     7 14 21 28        4 11 18 25        2  9 16 23 30     6 13 20 27       3
Tu  1  8 15 22 29        5 12 19 26        3 10 17 24 31     7 14 21 28       4
We  2  9 16 23 30        6 13 20 27        4 11 18 25     1  8 15 22 29       5
Th  3 10 17 24 31        7 14 21 28        5 12 19 26     2  9 16 23 30       6
Fr  4 11 18 25        1  8 15 22 29        6 13 20 27     3 10 17 24 31       7
Sa  5 12 19 26        2  9 16 23 30        7 14 21 28     4 11 18 25      1   8
Su  6 13 20 27        3 10 17 24        1  8 15 22 29     5 12 19 26      2   9
   18 19 20 21 22    22 23 24 25 26    26 27 28 29 30 31 31 32 33 34 35  35  36 

One approach is to create multiple batches that combined create the schedule you want. For instance first simply select the weekdays of the last two weeks of June and then all week days of July and August in a second job and the first days of september as a third:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |

* * 18,19,20,21,22,25,26,26,28,29 jun * php /app/cmd import >> /logs/cron-offers.log
* * * jul,aug mon,tue,wed,thu,fri       php /app/cmd import >> /logs/cron-offers.log
* * 3-7 9 *                             php /app/cmd import >> /logs/cron-offers.log

Another option is to simply run your batch daily and use the more powerful syntax of a proper programming/scripting language to let the batch process itself (i.e. your PHP code) determine if it should execute or exit immediately.

HBruijn
  • 72,524
  • 21
  • 127
  • 192