2

I have cronjob to be run on every month first friday evening i used the below mentioned entry

00 20 1-7  * Fri         [ "$(date '+\%a')" = "Fri" ] && $HOME/path/to/my/script.sh > /dev/null 2>&1

This entry should run my script if Friday falls withing 1-7 day of the month, but my script is getting executed even after 7th (i.e on all Fridays of the month). Please suggest how to fix it.

1
  • 2
    Have you tried "5" instead of "Fri" on that line. Don't think that "Fri" works.
    – Alvin Bunk
    Commented Jun 14, 2014 at 6:19

1 Answer 1

4

This is because when you specify a day of month and day of week, cron will execute the job when EITHER of those constraints are true. From the man page for crontab (5):

   Note: The day of a command's execution can be specified by two fields —
   day  of  month,  and day of week.  If both fields are restricted (i.e.,
   aren't *), the command will be run when either field matches  the  cur‐
   rent time.  For example,
   ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st
   and 15th of each month, plus every Friday.

There isn't a direct way in cron to do what you want, but cron : how to schedule to run first Sunday of every month describes a workaround by using cron to run your script e.g. every Friday and then calculating in the script if the day of month is in the range 1-7, and only continuing when that is the case.

In response to the comment about using 5 rather than Fri to specify day of week: using Fri is OK, as the man page says:

   Months or days of the week can be specified by name.
1
  • Thanks for the explanation, i have written a simple script to fulfill my requirement Commented Jun 14, 2014 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.