After log rotation mongod.exe still uses rotatetd files

Hello!
I used mongo 4.2 on windows server 2019.
After i executed powershell command “.\mongo localhost/admin -u $user -p $password --eval “db.runCommand({logrotate : 1})””, I cann’t delete old files.
Also I see in resource monitor that old files are deing used mongod.exe.
And that’s why I have a question: what should I do to resolve those files?

Hi @Eugen_Saprykin

The command needs to be run on the admin database. Change the db.runCommand(.... to db.adminCommand(...

Hope this helps.

1 Like

Hi! It doesn’t help. It seems this is the same situation: Delete or Empty MongoDB log file. Some time our database server becomes… | by Ankit Kumar Rajpoot | DataDrivenInvestor

how do you run mongod for logrotate? “reopen” or “rename” mode?

is it possible you are trying to delete files from a non-windows-admin account that is not related to mongod admin?

I don’t specify logrotate option. I ran powershell as windows admin account, but this account is not related to mongo admin, that’s why I specify user and password for ps script.

here are a few things:

1- it is possible you were getting an error but couldn’t notice it: the command you are using should be {logRotate : 1}, capital “R”. apply this correction and try again.

2- I have spun up a test server with the following commands, the second also requires adding admin user (in a test folder). applying your command causes a log flush to disk with a date as the extension, and I had no problem removing those files. I could even delete the “mylogs” file while the server was still up ( server logs nothing if it not there, I had to create an empty one so server could continue printing logs).

mongod --dbpath ./data --logpath mylogs --logRotate rename
mongod --dbpath ./data --auth --logpath mylogs --logRotate rename

So I suggest first checking your disk for problems and also the system itself. files getting locked even to admins is not uncommon in windows systems.

If you really need to remove those files, try stopping/restarting the mongod. but if the problem persists you can say it is a disk/windows problem.

although its relevance is low, do not forget to also check the folder permissions of your logs.

PS: there might be other causes I missed, so do not take this post as an absolute solution.

To add to @Yilmaz_Durmaz’s reply.

If you run the command interactively do you get the {"ok":1} output. This would be the indicator that logrotation is completing.

What are the filename(s) you are attempting to delete?

By default the logrotate is going to rename the current log file by appending a timestamp to the filename and opening a new file with the name defined by --logpath or systemLog.path

A couple of screenshots of what you are experiencing can help clarify too.

I see {“ok”:1}. Logroatation comlete successful. In log path I see file that called mongo.log and creation time = script start time. Also in log path I see a new file mongo.log.YYYY-MM-DDTHH-mm-SS.
Modification time second file the same script starting time.
Also in file properties I see I have permission to all.
I just try to remove old file manual and it was successful.
Turns out that problem when i try remove files in the same script as logrotation.
I think the problem not in delay, becouse in script i try to delete files older than 7 days.
Here is full text script

$mongoBin = $args[0]

$user = $args[1]

$password = $args[2]

$serverName = “localhost”

try

{
Write-Host “Move to mongod location: $mongoBin”
Set-Location $mongoBin
.\mongo $serverName/admin -u $user -p $password --eval “db.adminCommand( { logRotate : 1 } )”
if (!$?)
{
“An error during changing log file”
Write-Host $Error[0]
Exit 1
}
#delete files older then 7days
$config = Get-ChildItem …\mongo*.cfg -Recurse
$input = Get-Content $config
$input | foreach-object {If ($_ -match '\Wpath:’)
{
$logPath = ($_ -split “path:”)[1].Trim()
}
}
Get-Item "$logPath.
" | Where-Object {
if ($.LastWriteTime -lt (Get-Date).AddDays(-7))
{
Write-Host "remove file $
"
Remove-Item $_.FullName
}

}

}
catch
{
Write-Host -Exception $_.Exception

Write-Host POWERSHELLERROR
Write-Error [string]::Empty
Exit 1
}

Also I have just tried at local machine and it works as expected

it is fortunate this eliminates a disk problem :wink:

I don’t have experience with PowerShell. so it is either on the file removal portion of the code or somehow the script does not have enough privilege to delete the file when/where it starts.

by the way, resource monitor does not show live data, otherwise it was not possible to inspect by eye. resource usages are delayed (I don’t know how long) before they are removed from the list.

Get-Item "$logPath." | Where-Object {
if ($.LastWriteTime -lt (Get-Date).AddDays(-7))

The filter is files last written more than 7 days ago. Its not going to remove the log file that was just rotated.

1 Like

I understand. I donn’t want remove just rotated files but files are created 7 days ago.

Try a different file property, creationtime.

I think your logrotation question is answered at least, you’re into powershell territory now.