logrotate configured by mere mortals

This will provide a way to implement log file rotations by a non-privileged users using the standard Linux logrotate infrastructure.

For our test case, we set up a log file that looks similar to a “normal” log file we may want to periodically roll over.

howards@howards:~$ i=1
howards@howards:~$ while [ $i -le 100 ]; do echo $(date) $i iteration >> my.log; i=$(($i + 1)); done
howards@howards:~$ tail -5 my.log
Sun Feb 28 11:49:41 EST 2010 96 iteration
Sun Feb 28 11:49:41 EST 2010 97 iteration
Sun Feb 28 11:49:41 EST 2010 98 iteration
Sun Feb 28 11:49:41 EST 2010 99 iteration
Sun Feb 28 11:49:41 EST 2010 100 iteration
howards@howards:~$

To archive this log and create a new empty version for ongoing activity, we need a logrotate.conf file in a user accessible location with read/write permissions.

I put mine under /home/howards, with the contents below…

/home/howards/my.log {
missingok
daily
compress
delaycompress
create 0644 howards howards
rotate 7
}

RTFM for the meaning of each parameter above, but they are probably self explanatory, anyway.

We can then begin running our logrotate as shown below. Keep in mind the following two things:

  • the –force parameter is not necessary except for when testing.
  • the –state parameter is necessary as non-privileged users will not be able to write to the default location
    of /var/lib/logrotate/status
howards@howards:~$ ls -lrt my.log*
-rw-r--r-- 1 howards howards 4192 2010-02-28 11:52 my.log
howards@howards:~$ logrotate --force logrotate.conf --state=mystate
howards@howards:~$ ls -lrt my.log*
-rw-r--r-- 1 howards howards 4192 2010-02-28 11:52 my.log.1
-rw-r--r-- 1 howards howards    0 2010-02-28 11:52 my.log
howards@howards:~$ i=1
howards@howards:~$ while [ $i -le 100 ]; do echo $(date) $i iteration >> my.log; i=$(($i + 1)); done
howards@howards:~$ logrotate --force logrotate.conf --state=mystate
howards@howards:~$ ls -lrt my.log*
-rw-r--r-- 1 howards howards  316 2010-02-28 11:52 my.log.2.gz
-rw-r--r-- 1 howards howards 4192 2010-02-28 11:52 my.log.1
-rw-r--r-- 1 howards howards    0 2010-02-28 11:52 my.log
howards@howards:~$

Put something similar to the following line in your user crontab, and you should be good to go.

0 0 * * * /usr/sbin/logrotate /home/howards/logrotate.conf --state=/home/howards/mystate

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.