crontab entry to run every last day of the month

i've created a script which should run every last day of the month. what would be the exact crontab entry for this? thanks!

Have cron run a script on days 28, 29, 30 and 31 of every month.
Create two variables in the script, one containing today's day of the
month and another containing tomorrow's day of the month:

 TODAY=`date +%d`
 TOMORROW=`date +%d -d "1 day"`

 # See if tomorrow's day is less than today's
 if  [  $TOMORROW  -lt  $TODAY  ];  then
         echo  "This is the last day of the month"
 #       Do stuff...
 fi

:cool:

to be ran at 11pm on the last day of a month:

00 23 * * *[ `date +%d` -eq `echo \`cal\` | awk '{print $NF}'` ] && myJob.sh
1 Like

You can try as,

00 23 * * * [[ $(date +'%d') -eq $(cal | awk '!/^$/{ print $NF }' | tail -1) ]] && job 1>/dev/null 2>&1

hth.

1 Like

Just a note - when using commands in cron always use the full path to commands...

/usr/bin/date
/usr/bin/cal

In this case however, the commands should work without the full path because cron sets up a very basic environment that usually includes /usr/bin in PATH, but I always err on the side of caution and provide absolute paths....

</pedant>

Cheers
ZB

Was to turn the date math trick shown above into a script that I named 'last-day-of-month.sh':

#!/bin/bash

TODAY=`/bin/date +%d`
TOMORROW=`/bin/date +%d -d "1 day"`

# See if tomorrow's day is less than today's
if [ $TOMORROW -lt $TODAY ]; then
        exit 0
fi

exit 1

Which can then be used within a crontab file as follows:

12 0 * * * last-day-of-month.sh && /run/my/cron/job.sh

I hope this is of some use to you...

-jason

I had a similar dilemma where I need to run a backup script every 2nd Tuesday of the Month. So I added the following to my script:

TODAY=`/bin/date +%d | cut -d"0" -f2`
TUE=`cal | awk {'print $3'} | xargs | /usr/bin/cut -d" "  -f3`
#See if today matches the 2nd Tuesday of the month
if [ "$TODAY" -ne "$TUE" ] ; then

        exit 0

else

Maybe just use something like this ...

if [[ $(date +%a) = "Tue" ]] && [[ $(date +%d) -ge 8 ]] && [[ $(date +%d) -le 14 ]]
then 
     echo 2nd Tuesday
fi
00 08 * * * [ `date +%d` -eq `echo \`cal | awk '{print $NR}'\` | awk '{print $NF}'` ] && /your_script_path/your_script.sh

A bit of explanation:
----------------------
cal : Gives the Calendar of that very month
$NR: Number of Records
$NR: Number of Fields

using "awk" command we are first getting the last line of Calendar Matrix, and then getting the last field of that line viz. last day of that month.

and Finally equating with the current Date

Hope this helps
Naveen Sharma

1 Like

I needed to run a script on the last day of the month as well. This is what I use....

#! /bin/ksh
# script to check for tomorrows date, usefull for running
# processes on certain day of the month; 
# TZ=CST+24 date +%Y-%m-%d (yesterday)
# TZ=CST-24 date +%Y-%m-%d (tomorrow)

if test `TZ=CST-24 date +%d` = 1; then
   myscript.sh
fi

Then put in a crontab entry like so

0 23 * * * /location/of/script/myscript.sh 2>&1 >> endofmonth.log