Is there a command for the cron job to run after every 14th of a month which should be a Saturday(only on the first Saturday after 14th or the third Saturday of the month) at 11.59 pm

I need to know the command of the cron job

59 23 * * 6 [ "$(date +%d -d tomorrow)" -gt 14 ] && [ "$(date +%u -d tomorrow)" -eq 6 ] && the task

I have used the above expression. but still unsure if any other expression can do the same job more efficiently.

2

There are 2 best solutions below

7
Peter Whittaker On BEST ANSWER

UPDATE: @jhnc found that the spec (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html) indicates that while there is a lot of "ANDing", sometimes there is "ORing": "Finally, if either the month or day of month is specified as an element or list, and the day of week is also specified as an element or list, then any day matching either the month and day of month, or the day of week, shall be matched."

Based on that, I think your original approach is probably the right one.

ORIGINAL ANSWER:

cron fields are "anded" together, and ranges are permitted, so this should work:

59 23 14-31 * 6 <the task> 
0
glenn jackman On

The first Saturday after the 14th is somewhere in the range 15 to 21:

01 00 * * 6 d=$(date '+%-d'); [ "$d" -ge 15 ] && [ "$d" -le 21 ] && the task

The hyphen in %-d is intended to remove the zero-padding from the day so 08 and 09 are not interpreted as invalid octal numbers. Check your date man page.

I'm confused why you're launching the cron entry at 23:59 and checking tomorrow, so I launched at 00:01 and checked today.

The cron entry is already constrained by day 6, so I'm not double checking that with date.