[ale] First three Fridays of a month via a Bash script?

James Sumners james.sumners at gmail.com
Wed Jun 6 13:07:37 EDT 2012


Yep. I just have it run every Friday. First thing in the script is:

----

if [[ "$(date +%a)" == "Fri" && $(date +%d) -le 21 ]]; then
  # This is only going to run on the first three Fridays
  # of every month
  exit
fi

----

I had thought of that solution before, but I wasn't sure of a date
that worked as a cutoff. Now I just need to modify it to only run on
the last Friday of the month for my production systems.

For the curious, this is the script:

-----

#!/bin/bash

if [[ "$(date +%a)" == "Fri" && $(date +%d) -le 21 ]]; then
  # This is only going to run on the first three Fridays
  # of every month
  exit
fi

EMAIL_SUBJ="updates available for $(hostname -s)"
EMAIL_TO="email at example.com"
EMAIL_BODY='/tmp/email.bdy'

# Get the list of all available package updates, skipping
# the first blank line of output.
yum -q check-update 2>/dev/null | tail -n+2 > ${EMAIL_BODY}

# Let's tell ourselves how many updates are available
COUNT=$(cat ${EMAIL_BODY} | wc -l | awk '{print $1}')
EMAIL_SUBJ="${COUNT} ${EMAIL_SUBJ}"

# Add a separator.
echo -e "\n\n----------\n\n" >> ${EMAIL_BODY}

# Get detailed information on the available security updates,
# skipping the yum repo updates info lines.
yum info-security 2>/dev/null | tail -n+4 >> ${EMAIL_BODY}

cat ${EMAIL_BODY} | sed 's/\n/\r\n/g' | mail -s"${EMAIL_SUBJ}" "${EMAIL_TO}"

rm ${EMAIL_BODY}

-----

Yeah, there is yum-updatesd and such. But yum-updatesd takes an
integer as seconds for its run interval (ridiculous), and I'm trying
to stagger updates between testing and production. I also don't want
to be driven crazy by update emails every day.

On Wed, Jun 6, 2012 at 12:31 PM, JD <jdp at algoloma.com> wrote:
> On 06/06/2012 10:46 AM, James Sumners wrote:
>> I've been thinking on this one since yesterday and haven't come up
>> with anything. I have a Bash script that I want to run on the first
>> three Fridays of every month. I don't think I can setup such a weird
>> schedule in Cron, so I'm thinking of just running it every Friday and
>> the script will exit without doing anything if it isn't an appropriate
>> day.
>>
>> My preference is to stick with standard utilities and Bash without
>> resorting to something like Python. The problem I'm encountering is
>> that GNU date does relative calculations either from the current
>> system time or from a file's modification time. This means I can't use
>> a contrived date such as the first of every month to do something like
>> `date -d 'first Fridate'`, `date -d 'second Friday'`, and `date -d
>> 'third Friday'` because I can't supply two '-d' parameters (one for
>> the start date and a second for the desired calculation).
>>
>> So, does anyone have any suggestions on how I can do this?
>
> Actually, I think you can run this from cron.
>
> The boundary conditions are when the 1st of the month is on a Friday, like June
> 2012 and again when the 1st of the month is on a Saturday, like Sept 2012.  The
> 3rd Friday will never happen after 21st - so any Friday before the 22nd of the
> month is in your range of 1st, 2nd, 3rd Friday.
>
> How would a crontab be written to achieve that?
>
> # .---------------- minute (0 - 59)
> # |  .------------- hour (0 - 23)
> # |  |  .---------- day of month (1 - 31)
> # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
> # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
> # |  |  |  |  |
> # *  *  *  *  *  command to be executed
> 0 4 1-21 * 5 /path/to/command
>
> Run at 4am, right?
>
> Well, according to the man page ... I'm wrong.  That would run every day from
> 1-21 AND every Friday - not what we want.  The best answer leveraging cron is to
> either
> a) run the command either every day less than 22 and return for non-Fridays
> or
> b) run only on Fridays, but if the day-of-the-month is over 21, return.
>
> I'd be inclined to use method "b", but I'd document it all over the place.


-- 
James Sumners
http://james.roomfullofmirrors.com/

"All governments suffer a recurring problem: Power attracts
pathological personalities. It is not that power corrupts but that it
is magnetic to the corruptible. Such people have a tendency to become
drunk on violence, a condition to which they are quickly addicted."

Missionaria Protectiva, Text QIV (decto)
CH:D 59



More information about the Ale mailing list