copytruncate failing because file is being used by another process
blairzanon opened this issue · comments
I have a log file constantly being written to by an application, and have attempted to use copytruncate so that the original log file can continue to be used. I receive this error in the Log-Rotate.log
Truncating C:\utils\stunnel\stunnel.log
Process-Local-Block : Exception calling "RotateMainOnly" with "0" argument(s): "The running command stopped because
the preference variable "ErrorActionPreference" or common parameter is set to Stop: The process cannot access the file
'C:\utils\stunnel\stunnel.log' because it is being used by another process."
At C:\Program Files\WindowsPowerShell\Modules\Log-Rotate\1.5.3\public\Log-Rotate.ps1:207 char:13
+ Process-Local-Block -block $block @blockoptions
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], MethodInvocationException
+ FullyQualifiedErrorId : ScriptMethodRuntimeException,Process-Local-Block
My stunnel-log.conf file is:
"C:\utils\stunnel\stunnel.log" {
rotate 31
notifempty
missingok
dateext
dateformat -%Y%m%d-%s
copytruncate
compress
compresscmd C:\Program Files\7-Zip\7z.exe
}
I believe this happens because copytruncate
is not correctly implemented. The actual behavior should allow a low level shifting of the file offset using O_APPEND
, see https://unix.stackexchange.com/questions/475524/how-copytruncate-actually-works. I'm not sure how to implement this in powershell, so assume that copytruncate
is non-functional.
A workaround is to stop that process using prerotate
, and start the process using postrotate
. But that would mean downtime of the process for the duration of the rotation, and lost logs etc. Hope that helps.
using "" | Out-File $path -NoNewline -Encoding ASCII
instead of Clear-Content
could you be a solution