cronjob every 4 weeks, not executing

Solution 1:

"Wrong" shell used in cron

Your test seems command to use syntax which may be "misinterpreted" by some shells other than bash. Cron by default uses /bin/sh shell.

Possible fixes to choose from:

a) wrap test argument in single quotes and force "right" shell

0 13 * * 6 /bin/bash -c 'test $((10#$(date +%W)%4)) -eq 0' && …

b) move your test command to separate executable file with explicitly selected shell in shebang:

#!/bin/bash
exec test "$((10#$(date +%W)%4))" -eq 1

c) change shell used by cron. In crontab add line

SHELL=/bin/bash  

In your case specific case option "a" seems to be the best but similar problems may be best served by other options.

Solution 2:

I wonder if this would work:

30 13 * * 6/4 /bin/bash /backup/test.sh

I'm not sure if it'll function as desired, but if my understanding of man 5 crontab is right, it'll run every fourth Saturday (but possibly not the exact days as your code specifies).


Also note that you need to escape percent signs in crontabs and they can't do bashisms like base 10 via 10# (unless you use SHELL=/bin/bash at the top of your crontab or your /bin/sh is a symlink to /bin/bash).

Maybe try this instead:

30 13 * * 6   test $((1$(date +%%W)%%4)) -eq 1 && /bin/bash /backup/test.sh

This takes out the bashism and replaces it with a concatenation that otherwise absorbs the leading zeros (107%4 == 7%4 == 3 since 100 is divisible by 4). It also escapes the percent signs so crontab doesn't try to interpret them.