14

I need to find a way to cron a job so that it runs every second wednesday of the month. Is this possible?

4
  • 2
    This question looks to provide an appropriate answer.
    – scurker
    Jun 20, 2011 at 14:16
  • 1
    Yeah, but that question's about Tue, not Wed ;) The accepted answer is clever, though. Jun 20, 2011 at 14:22
  • >> Yeah, but that question's about Tue, not Wed ;) The accepted answer is clever, though ... what is so hard to change Tue to Wed ?
    – ajreal
    Jun 20, 2011 at 14:33
  • 0 * * * 3 test $(date \+%u) -eq 3 && echo "start run me" try this. didn't paste to the answer because one liner question is pretty vague.
    – Jasonw
    Jun 20, 2011 at 14:53

8 Answers 8

20

My manpage for crontab (which I sadly can't seem to find online) gives the following example:

# Run on every second Saturday of the month
0 4 8-14 * *    test $(date +\%u) -eq 6 && echo "2nd Saturday"

Adapting this to your purposes...

0 4 8-14 * *    test $(date +\%u) -eq 3 && job.sh
3
5

For CentOS 7 servers, this seems to be the syntax that works for me. Please note the spaces around the [ and ]. That took a while for me to figure out.

This runs the test.sh file at 13:07 / 1:07PM on the second Wednesday of the month. (0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, etc.)

07 13 8-14 * * [ `date +\%u` = 3 ] &&  /root/scripts/test.sh
1
  • Working for me on CentOS 8 Stream too, thanks.
    – Codemonkey
    May 19, 2022 at 17:30
1

Based on this answer, you could do:

00 12 * * Wed expr `date +\%d` \> 7 \& `date +\%d` \< 15 >/dev/null && runJob.sh
0

Fire at 10:15 AM on the third Friday of every month: 0 15 10 ? * 6#3

Source: https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm

1
  • That's so cool. Too bad it isn't supported on most common Linux distros :/ Feb 16, 2023 at 11:39
0

It's not possible using cron on its own, but you could call a script once a week that does the test:

In crontab, run second_wed.sh at 12.00 every Wednesday:

0 12 * * 3 /home/you/bin/second_wed.sh

In second_wed.sh:

#!/usr/bin/env bash

#get day of month
day_of_month=`date +%d`

#if this day is between 7th and 15th day of the month = 2nd week
if [ $day_of_month -gt 7 -a $day_of_month -lt 15 ]; then
  # Call your program here instead of 'ls'…
  ls
fi
0
0
7 13 8-14 * 3 /home/you/bin/second_wed.sh
0

I would try:

37 2 * * 3 [[ $(($(date +%-V -d '20220208') % 2)) -eq 0 ]] && /path/to/script

This will run the script on Wednesday of every even week of the year. Unfortunately you may hit one week delay when the year has 53 weeks.

-1

You can avoid running an external script by using a combination of the day of week trick plus the weekday:

# Run on every second Wednesday of the month
0 4 8-14 * Wed job.sh

This also avoids running another external program. The 8-14 selects all days that match the second week of the month. Then it filters out just that Wednesday.

5
  • 3
    according to this: crontab.guru , cron will execute the task every day: 8,9,10,11,...,14 at 4AM. So, it is 7 invocations instead of one. I don't want to test it in production)) May 31, 2020 at 14:52
  • @maxkoryukov it says "“At 04:00 on every day-of-month from 8 through 14 and on Wednesday.” will execute only once, when its Wed and the day in the range 8-14.
    – Gabriel
    Jun 8, 2020 at 22:04
  • 2
    @GabrielA.Zorrilla friend, please check the "next invocations" section. Or run something with this settings on your host. Jun 9, 2020 at 8:28
  • Or better yet, just read the man page: 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., don't start with *), the command will be run when either field matches the current 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.
    – Angelo
    Jun 10, 2021 at 5:07
  • In CentOS 8 Stream this certainly didn't work for me - as others have said it tries to run every date specified in the third column.
    – Codemonkey
    May 19, 2022 at 17:32

You must log in to answer this question.