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.
  • 416

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
  • 7,848
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
  • 432