Schedule Job Fortnightly (FRI) in Crontab

Solaris platform.
How can I schedule crontab job every 2 weeks and the job to be scheduled on Fri?

# "first" friday  -- and note # generally does work in crontab files as a comment
0 8 ** 6  [ $((  $(( $(date +%s ) / 86400  )) % 2 )) -eq 0 ] && /path/to/myjob
#  "second" friday
0 8 ** 6  [ $((  $(( $(date +%s ) / 86400  )) % 2 )) -eq 1 ] && /path/to/myjob

No Solaris platform available to test but this crontab entry :

0 10 * * 5 [ $(( $(date +'\%U') % 2)) -gt 0 ] && /usr/local/bin/your_script

Or run your script every friday and test week of year is odd with

if [ $(( $(date +%U) % 2 )) -gt 0 ]

Note: % has special meaning to many versions of cron and usually needed to be quoted

2 Likes

FWIW- % on a crontab line is POSIX for "end of line". Any lines after the % character are considered stdin

Hi All,

Thanks for the reply.
I'd tried as below, but encountered error.

0 8 ** 6 [ $(( $(( $(date +%s ) / 86400 )) % 2 )) -eq 0 ] && $HOME/admin/scripts/rjob.sh
crontab: error on previous line; unexpected character found in line.
crontab: errors detected in input, no crontab file generated.

---------- Post updated at 08:51 AM ---------- Previous update was at 08:41 AM ----------

I'm able to schedule with below syntax. Shall feedback the result..
0 10 * * 5 [ $(( $(date +'\%U') % 2)) -gt 0 ] && /usr/local/bin/your_script

---------- Post updated at 09:00 AM ---------- Previous update was at 08:51 AM ----------

[quote=chubler_xl;302848241]
No Solaris platform available to test but this crontab entry :

0 10 * * 5 [ $(( $(date +'\%U') % 2)) -gt 0 ] && /usr/local/bin/your_script

Or run your script every friday and test week of year is odd with

if [ $(( $(date +%U) % 2 )) -gt 0 ]

Tested using below:
at now [ $(( $(date +'\%U') % 2)) -gt 0 ] && /path/rjob.sh
ksh: \35 % 2: syntax error

You're making it too hard on yourself, just use:

if [ $(( $(date +%U) % 2 )) -gt 0 ]
then    /path/rjob.sh
fi

Also at now is not a valid way to test crontab entries particularly these ones with escaped percent symbols.

You are better off just writing a script that updates/appends to a logfile and scheduling it for 5 or 10 mins in the future and waiting for cron to run it.

eg if the current time was:

Tue, Sep 03 4:25 PM

Schedule a test for 4:30 PM :

30 16 * * *  [ $(( $(date +'\%U') % 2)) -gt 0 ] && echo "Script run" >> /tmp/test.log

Wait 5 mins and check /tmp/test.log

It's easier to make a crontab entry to run every Friday and have the 1st test in the script that is run kick off the real job if it is an even week (or run the real job if it is an odd week as shown in earlier posts).