Wednesday, May 28, 2014

Problem of the dangling output log file

LOG ROTATION



Many of you might have faced this issue.Lets say we have a script try.sh and we run it as follows


sh try.sh > output

or consider we have a web server which sends its logs to some log file .

Now everything is fine until the size of the log File is small.
But once it rises you may face disk full issues.
I faced this issue with my webserver.
Now if you try to manually truncate the file it will not work.
Since it has been opened for writing and is currently being written.
Deleting it and creating new file of the same name will not work since the node is attached to the script .

Then how can you rotate the log file and compress it.
Answer is use the linux utility logrotate.

For more details do
man logrotate

It needs a config file to get the log file to be rotated.
add a cron job as follows.

/usr/sbin/logrotate /etc/logrotate.conf


the block u must append in /etc/logrotate.conf

is
/home/script/output {
       daily
       rotate 12
       size 1G
       compress
       missingok
       notifempty
       copytruncate
}


here /home/script/output is the path of the output file to be rotated.

other fields mean the following
run it daily
keep log file of up to 12 days
rotate if size more than 1 GB
compress the log file after rotation.
do not worry if file is missing
notify if the file is empty
create a copy of it , and truncate the original file.


with this the original issue has been solved.
You can now save the disk space and rotate logs.





No comments:

Post a Comment

Note: Only a member of this blog may post a comment.