0

I have the following Cron rule:

0 8 1,2,3,4,5,6,7,14,15,16,17,18,19,20 * 2 /root/command.sh

My objective is to run the /root/command.sh on Tuesday every two weeks.

Crontab.guru returns the following:

At 08:00 on day-of-month 1, 2, 3, 4, 5, 6, 7, 14, 15, 16, 17, 18, 19, and 20 and on Tuesday.

and on Tuesday

But, for some reasons, the above task was run today (Friday).

For precision:

$> date
$> Fri Mar  3 10:04:21 UTC 2023

The server is correctly thinking we are Friday.

Also

$> cat /etc/issue
$> Debian GNU/Linux 11

I'm suspecting that somehow, the parameters given to Cron are not correct?

Precision: If you have a better way to tell Cron "run this on Tuesday every two weeks", I'm all ears!

Cyril N.
  • 406
  • 1
  • 8
  • 23
  • Does this answer your question? [crontab day of week vs. day of month?](https://superuser.com/questions/348348/crontab-day-of-week-vs-day-of-month) – Kamil Maciorowski Mar 03 '23 at 10:09
  • 1
    @KamilMaciorowski, IMHO this is not duplicate as it give some explanation and no solution for current case. – Romeo Ninov Mar 03 '23 at 10:30
  • IMO the problem here is not understanding `cron` properly. It's a bit confusioning, I certainly have issues with it. However the guys in ServerFault don't: https://serverfault.com/questions/633264/cronjob-run-every-two-weeks-on-saturday-starting-on-this-saturday – Peregrino69 Mar 03 '23 at 14:46

2 Answers2

2

The rule fro cron is it run day(s) of month OR day(s) of week. So in your case will run every Tuesday + 1,2,3,4,5,6,7,14,15,16,17,18,19,20 days of month.

To run every two weeks you should incorporate login in the script to run for example first and third week of month. For example as first lines in script add:

number=$(date +%W)
if [ $((number%2)) -eq 0 ]
then exit
else <run the command>
fi

And in cron to be

0 8 * * 2 command

This will run script 0, 2, 4 etc week of year, Tuesday. But this mean you can have situation where script is run 3 times in month. If you want to run on odd week (1,3,5,....) change the command for then and else or edit if like:

if [ $((number%2)) -eq 1 ]
Romeo Ninov
  • 5,319
  • 5
  • 20
  • 20
  • 1
    Maybe we could remove the `else ` and set the commands outside the if/fi. If the date is not an odd week and a tuesday, it will go in that condition and quit the script. Otherwise, it will continue after the condition, running the expected code – Cyril N. Mar 06 '23 at 10:02
  • @CyrilN, yes, that's possible. I just make the script to be more human readable :) – Romeo Ninov Mar 06 '23 at 10:03
0

I don't think there's a way to do this with a cron expression alone.

The usual solution is to use an expression that runs on every Tuesday (0 8 * * 2), and then check if it's an even or odd week number inside /root/command.sh.

PS. Some cron implementations support a TUE#2 syntax in the day-of-week field, and interpret it as "the second Tuesday of the month". For these, you could try 0 8 * * 2#1,2#3. But Vixie cron does not support this.

cuu508
  • 262
  • 1
  • 3
  • 10