LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How does logrotate work? (https://www.linuxquestions.org/questions/linux-software-2/how-does-logrotate-work-645229/)

kenneho 05-28-2008 01:33 AM

How does logrotate work?
 
Hi.


I've set up logrotate to rotate the log files from our java application. The log file is rotated whenever it hits the defined size limit.

As far as I know there are noe signals needed to be sent to the java application when one rotates the logfile, so I've simply done this:

/path/to/logfile.log {
missingok
notifempty
size=5M
rotate 20
}

What I was wondering is this: How does logrotate rotate the file? Does it rename the file to logfile.log.1 and create a new logfile, or does it copy logfile.log to logfile.log.1 and empty the logfile.log? In other words, will logfile.log remain with the same file system inode? I'm afaid that the logging will fail if the inode changes unexpectedly.

Regards,
kenneho

aus9 05-28-2008 01:59 AM

altho I am sure you are going to get a better answer, let me comment on your current style if I may.

missingok...no harm to leave in but I assume you have named the pathway to a real file so it can be deleted
notifempty...well I suggest rotation occurs due to your size command so if empty no rotate so can be deleted.

2) It may take some time to get to 5 megs so why not lower it and see what happens?

3) inodes may be an issue for you if your application truly requires it....I do not doubt you....but inodes themselves do not affect the actual rotation.

to see what your current inodes are try these commands in no particualr order

stat /var/log/(nameoflog)....which output inode info

ls -i /var/log.....will output inode and filenames but less info (if required) than stat

kenneho 05-28-2008 02:14 AM

Quote:

Originally Posted by aus9 (Post 3166683)
altho I am sure you are going to get a better answer, let me comment on your current style if I may.

missingok...no harm to leave in but I assume you have named the pathway to a real file so it can be deleted
notifempty...well I suggest rotation occurs due to your size command so if empty no rotate so can be deleted.

2) It may take some time to get to 5 megs so why not lower it and see what happens?

3) inodes may be an issue for you if your application truly requires it....I do not doubt you....but inodes themselves do not affect the actual rotation.

to see what your current inodes are try these commands in no particualr order

stat /var/log/(nameoflog)....which output inode info

ls -i /var/log.....will output inode and filenames but less info (if required) than stat

Thanks for the reply.

I set up a simple test regarding the inode issue, and found this: When logfile X rotates to X.1, the inode remains. It thus seems as logrotate merely renames X to X.1, and makes a new file X. I'm hoping this won't be a problem for the java application. Will test it now.

grizly 05-28-2008 02:27 AM

From:http://www.linux-mag.com/id/627
Quote:

When logrotate actually processes a log file, there are several phases to its activity. First, any commands in a prerotate section of the relevant configuration file entry are executed. Next, a numbered backup file is created. After that, any commands in the configuration entry’s postrotate section are executed (there are two examples above). Finally, the copied (old) log file is compressed (if requested). You can use the pre- and post-rotation hooks to perform whatever actions make sense within the context on your systems. For example, you could copy the oldest saved log file to a different location for backup to tape before it is overwritten by logrotate.
So yeah, it creates the .1 file first I guess. ;)

kenneho 05-28-2008 05:59 AM

I found that "copytruncate" option to logrotate ensures that the inode doesn't change. Basically what is was looking for.

pakseykin 08-24-2011 12:34 PM

Quote:

Originally Posted by kenneho (Post 3166830)
I found that "copytruncate" option to logrotate ensures that the inode doesn't change. Basically what is was looking for.

Once I also was looking for the explanation. So, I'll post it here.
- Option "copytruncate" helps to avoid issues when logrotate is allowed to move files instead (the issue is that the file handle used by the process generating logs is not be updated = file is not reopened).
- Some servers processes have notification interface through signals (see default configuration for httpd), others may have some exotic ways to be notified (or no way at all). If restart of the server is acceptable, stopping it in "prerotate" and starting it in "postrotate" works for sure.


All times are GMT -5. The time now is 03:17 PM.