Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I assume this is relatively easy but my shell scripting isn't that great so I'm hoping to get some help.

I have a task which runs every hour and appends its output to a log file. I'd like to move that log file every night so that the script has an empty one to work with and the logs are easier to look at when I need to check them.

So I'd like a script which moves

/var/log/my.log

to

/var/log/my.log.2 or my.log.yyyy.mm.dd or something.

How can I do that pretty easily?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You'll want to check out logrotate which is installed by default (or easily though a package manager) on nearly every major Linux distribution. It's quite easy to put together a simple configuration entry to rotate your log file -- everything you need to know about configuration options is on that man page.

Some distributions use a special /etc/logrorate.d/ directory to specify individual logrotate files for each log file (or a related group of log files). If your distribution uses this method, it's as simple as creating an appropriately-named file in that directory with a configuration block as described in the man page.

Otherwise you can simply just append your new configuration block to the end of the existing /etc/logrotate.conf file.

share|improve this answer
    
after I create the file in /etc/logrotate.d, will it run by default or do I need to set anything else up? –  Matt Feb 8 '12 at 4:02
    
Logrotate runs in a daily cron and re-reads configuration files for each run, so there's no need to take additional steps. –  Garrett Feb 8 '12 at 6:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.