[Linux]: How to avoid the growth of a file

Hi,
my O.S. is:
Linux version 2.6.18-308.1.1.el5 (Red Hat 4.1.2-52))

I have a log file "audit.log" that grows fast and when it exceeds the size of 5 gigabyte it blocks my application.
Now I'd like to create a script that does the compression of this file (i.e. audit.log.YYYYMMDD-HHMISS.gz) and I'd like to run it automatically every night (i.e. at  02:00 AM) and removes the old files every 2 months.

Can anyone tell me, in detail, how I can write this script?

Thanks in advance!
ralph_reaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MazdajaiCommented:
What distro?

You should not need to write custom script to rotate the logs, you can use logrotate.
0
TintinCommented:
Look at /etc/logrotate.conf and the /etc/logrotate.d directory for how other log files and compressed and rotated.
0
ralph_reaAuthor Commented:
I've never used the command logrotate.

What must I do if I want to write a castom script ?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

MazdajaiCommented:
If you have little or no experience in writing custom script, I recommend you to look at logtate and the reference I mentioned as it.
0
TintinCommented:
I wouldn't recommend you write a custom script when you have a standard utility to do it for you.

Simply create /etc/logrotate.d/audit with the following contents:

/var/log/audit.log {
  compress
  daily
  rotate 60
}


You may need to add some other options depending on the nature of the log file, but the above will probably be enough.
0
ralph_reaAuthor Commented:
So I do not have to edit the file /etc/logrotate.conf,  but only add a new file under the directory /etc/logrotate.d with these steps?

cd /etc/logrotate.d
vi audit_logrotate

/.........../audit.log {
  compress
  daily
  missingok
  notifempty
  rotate 60
}



correct?

Also, if I want to add the date and time the file? (audit.log.YYYYMMYY-hhmiss.gz)
0
TintinCommented:
Add

dateext
dateformat %Y%M%d%s


Note that logrotate can't use HHMMSS in the dateformat, so you need to use seconds from epoch in order to retain a sortable order.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.