I want to capture slow queries via logrotate, and I want them to rotate weekly and I want to save a year's worth. The logs take the form:

-rw-r-----. 1 mysql  root           1239 Feb 21 18:46 mysqld1-slow.log
-rw-r-----. 1 mysql  root            885 Feb 11 14:48 mysqld2-slow.log
-rw-r-----. 1 mysql  root            885 Feb 22 08:58 mysqld3-slow.log
-rw-rw-rw-. 1 mysql  root            802 Feb 11 14:47 mysqld-slow.log

Because the logs end up being written to so frequently, how can I make sure nothing is missed by logrotate? The process itself doesn't create the file, it needs to have the original there. I was thinking this would do it:

/var/log/mysqld*-slow.log {
    rotate 52
    create 0644 mysql root

So it should compress the old, and create the same filename with the right permissions, but I'm unsure how logrotate handles something that's written to amidst the movement.

1 Answer 1


It would appear the copytruncate function can accomplish this:

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 cannot 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 logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.

I have put this in place.

  • How does this resolve the data loss issue you highlighted in your question since the copytruncate documentation as you have cited warns about the possibility of data loss? Nov 16, 2021 at 14:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .