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!

log rotation based on month

Status
Not open for further replies.

cdeardor

MIS
Oct 25, 2002
49
US
I'm writing a script to rotate our syslogs. It used to be done by days, but now they want to keep 2 months online. So, for instance, if it's April, we'll archive February's logs. My problem is that I'm not sure how to manipulate the Month string from the date command, so that in January, and February, the script doesn't blow up. Any suggestions?
 
You don't really need to manipulate a date string. You can use the [tt]find[/tt] command to select files based on their last modification or last access date and time.

Something like...
Code:
#!/bin/ksh

LOGDIR=/path/to/logs

find ${LOGDIR} -name '*.log' -mtime +60 -print | while read LOGFILE
do
    print "Log file to process: ${LOGFILE}"

    # do processing stuff here
done
This would find and allow you to process log files over 60 days since it was last modified. This works well over year end boundaries because it's based on the file's "age", not a date stamp of any kind.

You can also keep a file around longer if you want by just doing a "[tt]touch somefile.log[/tt]".

Hope this helps.
 
Thanks for the reply, however, that's how I originally had it done. Now, they don't want to go by 60 days, they want it keyed off of the month. So, 60 days won't work for me because of February only being 28 days, and also the 31 day months.
 
Did you consider -newer option of find command?
Or to be more exact, its negation, ! -newer ?
 
That's a good idea, too. I definately didn't think of that one. I decided to just use case statements, and limit it to one of syslogs that can be kept online, after that, they get archived. I think I'll try some logic with the find command, though. Maybe that'll find it's way into the next version. Thanks! My big problem is that this is being done on AIX. For all the linux users out there, Here's a good command that would work with what I was trying to do:

date --date="2 month ago" +%b%d%Y

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top