[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 849
  • Last Modified:

Logrotate question

Hi,
I would like to archive my tomcat access logs, or at least compress them.  Tomcat is by default rotating  the logfiles, but its not compressing them.  

Logrotate could probably help me here, but  in  the examples I have seen, you need to restart the service after each rotation and this is a no go for me because we are talking production servers.

Apache example:
/var/log/apache/*.log {
        weekly
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                if [ -f /var/run/apache.pid ]; then
                        /etc/init.d/apache restart > /dev/null
                fi
        endscript
}


Do i really need to restart the apache (or Tomcat in my case)  service after each log rotation?

I would just use logrotate to compress them as tomcat already  rotates the logs everyday.
0
icongo
Asked:
icongo
  • 2
  • 2
1 Solution
 
woolmilkporcCommented:
Just replace the whole "postrotate" block (up to "endscript") with

copytruncate

This is from logrotate's man page:

      copytruncate
              Truncate  the  original log file in place after creating a copy,
              instead of moving the old log file and optionally creating a new
              one,  It  can be used when some program can not be told to close
              its logfile and thus might continue writing (appending)  to  the
              previous log file forever.  Note that there is a very small time
              slice between copying the file and truncating it, so  some  log-
              ging  data  might be lost.  When this option is used, the create
              option will have no effect, as the old log file stays in  place.
http://linuxcommand.org/man_pages/logrotate8.html

Now keep tomcat from rotating on its own and you should be done.
0
 
icongoAuthor Commented:
I have used copytruncate, it truncates fine.  But what if I want to delete the files that are truncated after  30 days?
Logrotate  changes the timestamp on all files  when it runs so all log files with size 0 have a timestamp of last logrotate runs..

If i run this:
$PATH/log/tomcat/*.log {
    daily
    rotate 10
    copytruncate
    compress
    notifempty
    missingok
    sharedscripts
    postrotate
    find $PATH/log/tomcat/  -name "*log*" -mtime +30 -exec /bin/rm -f {} \;
    find $PATH/log/tomcat/  -size -1 -exec  /bin/rm -f {} \;
    endscript

Thanks.
0
 
woolmilkporcCommented:
The truncated files keep being used by tomcat and should not stay at size 0 for a long time,
so what are you trying to delete?

According to your settings the old logs are kept for 10 days before being removed, so you should never see any rotated logs older than 10 days.

You even use the "notifempty" parameter, so logs of size 0 (besides maybe the current one) are virtually impossible.
0
 
icongoAuthor Commented:
THanks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now