The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Crontab Entry Dastard Shell Programming and Scripting 5 02-09-2007 11:22 PM
crontab entry Sowser UNIX for Advanced & Expert Users 4 01-08-2007 02:34 PM
Crontab entry bestbuyernc Shell Programming and Scripting 1 09-12-2005 12:13 PM
crontab entry matrixmadhan UNIX for Dummies Questions & Answers 3 06-07-2005 05:57 AM
Crontab First Monday of Month only molonede UNIX for Dummies Questions & Answers 3 10-09-2001 06:38 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 06-08-2005
Registered User
 

Join Date: May 2005
Posts: 40
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!
Reply With Quote
Forum Sponsor
  #2  
Old 06-08-2005
Registered User
 

Join Date: May 2005
Posts: 46
Cool

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

Reply With Quote
  #3  
Old 06-09-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
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
Reply With Quote
  #4  
Old 06-09-2005
Registered User
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
You can try as,

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

hth.
Reply With Quote
  #5  
Old 06-09-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
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
Reply With Quote
  #6  
Old 08-26-2007
Registered User
 

Join Date: Aug 2007
Posts: 1
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
Reply With Quote
  #7  
Old 09-09-2008
jsandova's Avatar
Registered User
 

Join Date: May 2006
Location: Burbank Ca
Posts: 27
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0