Linux – How to continue redirecting stdout to a file after logrotate moves it

linuxlogrotateredirect

I have a simple script which outputs a bunch of logs to screen and I piped the STDOUT to a file to store the logs. Since this script is long running, I needed to rotate the log files so they are chucked into smaller more manageable ones.

The problem I faced was that once the logrotate moves the current log file into a new one, the newly created log file is not populated with the logs anymore. It seems that the once the original log file is removed, its file handler is lost and redirection won't work anymore.

I also found this post which had the same problem as me and claims that it can be fixed by using >> instead of > to redirect the output. I tested his solution but it didn't work for me. Does anyone have any idea how to keep the redirection work?

Best Answer

You should use the copytruncate directive in your logrotate config for this log file.

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