Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need cronjob on last day of the month 3

Status
Not open for further replies.

redsss

Programmer
Mar 17, 2004
72
US
I need to run a cronjob on the last day of each month. Any suggestions?

Does cron SKIP running a job thats set to run on the 31st of every month but there are only 30 days in the month?
 
You may consider every first day of month at 0 hour (ie on the last day of each month at 24 hours):
0 0 1 * * Your cron job

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
You could just put 12 entries in your crontab, but of corse this wouldn't get leap years.

Which O/S are you using, it's easier on some than others

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Perhaps have cron run the script on the 28,29,30,31 days of every month. The script would determine how many days there are in the current month. e.g using cal...
Code:
#!/usr/bin/ksh

set -A arr $(cal)
days_in_month=${arr[$((${#arr[*]}-1))]}
day_of_month=$(date +%d)
if [[ $days_in_month -eq $day_of_month ]]
then
   echo run the script
else
   echo not the last day
   exit
fi
 
Another way of getting the last day using cal:
Code:
val=$(cal)
val=${val##* }
Or
Code:
val=$(echo $(cal) | awk '{print $NF}')
etc... :)
 
Toolkit - I love that first snippet of code, but can I sound dumb by asking, what is the ##* at the end of val. I am assuming this is to access the last element in the list "val", but how would I access the first element, or the 10th element?
Just curious...

Thanks!

Brian
 
man ksh, the variable substitution chapter.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Wow! Thanks for the fast response! I looked at the manpage for ksh and that does explain it well!

Thanks again - stars for both of you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top