crontab entry to run every last day of the month | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

crontab entry to run every last day of the month

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-08-2005
tads98 tads98 is offline
Registered User
 
Join Date: May 2005
Last Activity: 15 October 2009, 6:19 AM EDT
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Sponsored Links
    #2  
Old 06-09-2005
rahul123_libra rahul123_libra is offline
Registered User
 
Join Date: May 2005
Last Activity: 13 November 2006, 1:04 AM EST
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
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



Moderator's Comments:
Code tags please, thanks.

Last edited by zaxxon; 10-20-2011 at 03:54 AM.. Reason: code tags
Sponsored Links
    #3  
Old 06-09-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 25 July 2014, 9:34 AM EDT
Location: Foxborough, MA
Posts: 7,644
Thanks: 150
Thanked 577 Times in 542 Posts
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

The Following User Says Thank You to vgersh99 For This Useful Post:
tinufarid (03-14-2013)
    #4  
Old 06-09-2005
muthukumar muthukumar is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 8 November 2005, 6:07 AM EST
Location: Coimbatore, Tamilnadu, India
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
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:
Code tags please, thanks.

Last edited by zaxxon; 10-20-2011 at 03:55 AM.. Reason: code tags
The Following User Says Thank You to muthukumar For This Useful Post:
tinufarid (03-14-2013)
Sponsored Links
    #5  
Old 06-09-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
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
Sponsored Links
    #6  
Old 08-26-2007
jbuberel jbuberel is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 17 October 2008, 8:13 PM EDT
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
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
The Following User Says Thank You to jbuberel For This Useful Post:
Vinothjanarthan (03-12-2013)
Sponsored Links
    #7  
Old 09-09-2008
jsandova's Avatar
jsandova jsandova is offline
Registered User
 
Join Date: May 2006
Last Activity: 2 February 2011, 4:29 PM EST
Location: Burbank Ca
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
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:
code tags please, thanks.

Last edited by zaxxon; 10-20-2011 at 03:55 AM.. Reason: code tags
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
crontab entry to run a script on 1st of every month. deepaksahni0109 Solaris 5 04-02-2010 11:30 AM
crontab entry rajip23 Solaris 1 03-26-2009 07:38 AM
crontab entry udelalv AIX 2 02-26-2009 07:55 PM
Crontab Entry Dastard Shell Programming and Scripting 5 02-10-2007 01:22 AM
crontab entry Sowser UNIX for Advanced & Expert Users 4 01-08-2007 04:34 PM



All times are GMT -4. The time now is 11:15 AM.