Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copytruncate failing because file is being used by another process #52

Closed
blairzanon opened this issue Jul 21, 2022 · 2 comments · Fixed by #67
Closed

copytruncate failing because file is being used by another process #52

blairzanon opened this issue Jul 21, 2022 · 2 comments · Fixed by #67
Assignees
Labels

Comments

@blairzanon
Copy link

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
}
@leojonathanoh leojonathanoh self-assigned this Jul 22, 2022
@leojonathanoh
Copy link
Member

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.

@hche608
Copy link

hche608 commented Feb 6, 2024

using "" | Out-File $path -NoNewline -Encoding ASCII instead of Clear-Content could you be a solution

https://stackoverflow.com/questions/46724371/delete-clear-opened-text-file-which-is-used-by-another-process

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants