Page 1 of 1

Combine daily access logs to single monthly log cron job

Posted: Sun Dec 16, 2007 7:57 pm
by JAB Creations
I just added this to a sever I use though am waiting for the cron job to execute.

My question though is how can I amend the file name to include the numerical month and year?

So I want the monthly access log files to look like...
mydomain.com-2007-01.gz
mydomain.com-2007-02.gz
mydomain.com-2007-03.gz
mydomain.com-2007-04.gz

Here is the basic code. I don't know if this will generate a log file that grows so long as you leave the original daily access logs though? If I get this working I'm so going to post a whole page on my site dedicated to conquering this issue alone!

Code: Select all

zcat access.log.*.gz > monthlylog.txt
gzip monthlylog.txt

Posted: Tue Dec 18, 2007 7:57 pm
by JAB Creations
No one here has any idea how to use a Unix console?

Posted: Tue Dec 18, 2007 10:08 pm
by Kieran Huggins
You could have a daily cron job that creates tomorrow's log file as a link to it's monthly log. That way you won't need to combine them, the logger will simply append to the monthly one through the link. You could use the same cron to tear down the link from yesterday to keep things tidy.

Does that make any sense?

I could break out my Bash book if you want code.

Posted: Tue Dec 18, 2007 10:23 pm
by JAB Creations
That would be most appreciated Kieran! Anything you think that would be constructive towards achieving this goal would be great!

Posted: Tue Dec 18, 2007 11:05 pm
by Kieran Huggins

Code: Select all

# set the domain (I assume it will be looped  for many domains)
DOMAIN="domain.com"

# create tomorrow's monthly log if it doesn't exist
touch `date --date='tomorrow' +$DOMAIN-%Y-%m.log`

# create tomorrow's daily log as a link
ln -s `date --date='tomorrow' +$DOMAIN-%Y-%m.log` `date --date='tomorrow' +$DOMAIN-%Y-%m-%d.log`

# now remove yesterday's daily log file
rm `date --date='yesterday' +$DOMAIN-%Y-%m-%d.log`