crontab entry to run every last day of the month

Tags
shell scripts

 
Thread Tools Search this Thread
# 1  
Old 06-08-2005
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!
# 2  
Old 06-09-2005
Tools

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:

Code:
 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

Smilie

Moderator's Comments:
Mod Comment Code tags please, thanks.

Last edited by zaxxon; 10-20-2011 at 04:54 AM.. Reason: code tags
# 3  
Old 06-09-2005
to be ran at 11pm on the last day of a month:
Code:
00 23 * * *[ `date +%d` -eq `echo \`cal\` | awk '{print $NF}'` ] && myJob.sh

This User Gave Thanks to vgersh99 For This Post:
tinufarid (03-14-2013)
# 4  
Old 06-09-2005
You can try as,
Code:
00 23 * * * [[ $(date +'%d') -eq $(cal | awk '!/^$/{ print $NF }' | tail -1) ]] && job 1>/dev/null 2>&1

hth.

Moderator's Comments:
Mod Comment Code tags please, thanks.

Last edited by zaxxon; 10-20-2011 at 04:55 AM.. Reason: code tags
This User Gave Thanks to muthukumar For This Post:
tinufarid (03-14-2013)
# 5  
Old 06-09-2005
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
# 6  
Old 08-26-2007
My simple solution...

Was to turn the date math trick shown above into a script that I named 'last-day-of-month.sh':
Code:
#!/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:
Code:
12 0 * * * last-day-of-month.sh && /run/my/cron/job.sh

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

-jason
This User Gave Thanks to jbuberel For This Post:
# 7  
Old 09-09-2008
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:

Code:
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


Moderator's Comments:
Mod Comment code tags please, thanks.

Last edited by zaxxon; 10-20-2011 at 04:55 AM.. Reason: code tags

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Dynamic crontab entry for day and night kaushik02018 UNIX for Beginners Questions & Answers 4 04-29-2017 02:41 AM
Script to run on 2 4 and 7 day of the month tradingspecial Shell Programming and Scripting 9 06-16-2016 04:33 AM
Simpler crontab entry to execute pgm on last day of the month lsatenstein Shell Programming and Scripting 1 03-17-2015 08:17 AM
Julian day to dates in YEAR-MONTH-DAY Ggg Shell Programming and Scripting 1 06-19-2013 09:33 AM
Writing a script to run weekly/monthly - check for weekday or day-of-the-month newbie_01 Shell Programming and Scripting 1 04-14-2012 12:56 PM
Running Script via Crontab on 2nd Working day each month Hollo UNIX for Dummies Questions & Answers 5 11-09-2011 05:36 AM
run the script for last day of the month aks_1902 Shell Programming and Scripting 4 10-20-2011 03:43 AM
Script to counting a specific word in a logfile on each day of this month, last month etc pnara2 Shell Programming and Scripting 5 08-17-2011 05:00 PM
crontab: setup cronjob to run first wednesday of every month Anjan1 Shell Programming and Scripting 9 08-17-2011 01:34 PM
Crontab to skip only one entry in a day??? Prateek007 Shell Programming and Scripting 4 10-06-2010 08:55 PM
crontab entry to run a script on 1st of every month. deepaksahni0109 Solaris 5 04-02-2010 12:30 PM
Script to find previous month last day minus one day timestamp girish.raos Shell Programming and Scripting 3 09-03-2009 09:57 PM
cron script -run every 2nd day of month except Monday newtou UNIX for Dummies Questions & Answers 2 10-23-2008 07:13 PM
Cron to run first day of month to calculate date 3 months ago new2ss Shell Programming and Scripting 1 05-17-2007 03:28 AM
Write a shell script to find whether the first day of the month is a working day phani Shell Programming and Scripting 7 03-18-2005 01:59 AM