cron job to run on second to last friday each month

I needed a cron job to run on the second to last friday of every month. Our servers are running HP-UX, and the HP-UX date command is pretty basic and does not have all of the fancy options that Linux date command does, and it does not have the ability at all to return future dates. So I had to get creative. Here's the solution I'm using, just wanted to share it here in case it's useful for anyone else.

FYI the script is scheduled to run every friday via cron, and then the script determines whether or not it's the correct friday.

#!/bin/sh
  TODAY=`date +%d`
  LASTFRI=`cal | awk {'print $6'} | xargs | awk '{print $NF}'`
  SECONDLASTFRI=$(($LASTFRI - 7))
   
  if [ $TODAY -ne $SECONDLASTFRI ]; then
    echo "Today is NOT the second to last Friday of this month!"
    exit 1
  else
    echo "Today is the second to last Friday of this month!"
    echo 0
  fi
2 Likes