Multiple log files at 0 byte of logrotate

I configured logrotate, it’s seems to me that works but other the .gz files that contains the mongd.log there are files with 0 bytes in size of the past mongod.log files. What I have to do to have the compressed file fo the logs. Thank you in advance.

-rw-r----- 1 mongod mongod 0 Sep 18 00:00 mongod.log.2023-09-18T00-00-02
-rw-r----- 1 mongod mongod 0 Sep 19 00:00 mongod.log.2023-09-19T00-00-01
-rw-r----- 1 mongod mongod 0 Sep 20 00:00 mongod.log.2023-09-20T00-00-01
-rw-r----- 1 mongod mongod 0 Sep 22 00:00 mongod.log.2023-09-22T00-00-01
-rw-r----- 1 mongod mongod 0 Sep 23 00:00 mongod.log.2023-09-23T00-00-02
-rw-r----- 1 mongod mongod 0 Sep 24 00:00 mongod.log.2023-09-24T00-00-01
-rw------- 1 mongod mongod 32992 Sep 25 00:00 mongod.log-20230925_00.gz
-rw-r----- 1 mongod mongod 0 Sep 25 00:00 mongod.log.2023-09-25T00-00-01
-rw------- 1 mongod mongod 33062 Sep 26 00:00 mongod.log-20230926_00.gz
-rw-r----- 1 mongod mongod 0 Sep 26 00:00 mongod.log.2023-09-26T00-00-01
-rw------- 1 mongod mongod 33001 Sep 27 00:00 mongod.log-20230928_00.gz

systemLog:
destination: file
logAppend: true

logRotate: rename

path: /var/log/mongodb/mongod.log
timeStampFormat: iso8601-utc

/var/log/mongodb/mongod.log {
daily
rotate 5
dateext
dateformat -%Y%m%d_%H
compress
delaycompress
missingok
create640 mongod mongod
sharedscripts
postrotate
/bin/kill -USR1 cat /var/run/mongodb/mongod.pid 2>/dev/null >/dev/null 2>&1
endscript

Hello everything is fine? I hope so :smiley:

You can try to do it like this:

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
  logRotate: reopen
processManagement: 
  pidFilePath: /var/run/mongodb/mongod.pid

And logrotate.conf

/var/log/mongodb/mongod.log {
  daily
  size 100M
  rotate 10
  missingok
  compress
  delaycompress
  notifempty
  create 640 mongod mongod
  sharedscripts
  postrotate
    /bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod.pid 2>/dev/null` >/dev/null 2>&1
  endscript
}

This is just an example and you can adjust size, retention, among other things however you prefer.

1 Like

Hi @Enrico_Bevilacqua1,
As mentioned from @Samuel_84194, I think you need to add the parameter logRotate: reopen.

From documentation:

“You can also configure MongoDB to support the Linux/Unix logrotate utility by setting systemLog.logRotate or --logRotate to reopen. With reopen, mongod or mongos closes the log file, and then reopens a log file with the same name, expecting that another process renamed the file prior to rotation.”
https://www.mongodb.com/docs/manual/tutorial/rotate-log-files/#:~:text=You%20can%20also%20configure%20MongoDB%20to%20support%20the%20Linux/Unix%20logrotate%20utility%20by%20setting%20systemLog.logRotate%20or%20--logRotate%20to%20reopen.%20With%20reopen%2C%20mongod%20or%20mongos%20closes%20the%20log%20file%2C%20and%20then%20reopens%20a%20log%20file%20with%20the%20same%20name%2C%20expecting%20that%20another%20process%20renamed%20the%20file%20prior%20to%20rotation.

1 Like

First of all I wanto to thank you for your reply.
I’m sorry but the change proposed it didn’t work for me.
Following the mongod.conf section about the system log, the list of the files that I got and the logroate.d config file. Where am I wrong?

systemLog:
destination: file
logAppend: true
logRotate: reopen
path: /var/log/mongodb/mongod.log
timeStampFormat: iso8601-utc

total 992
-rw-r----- 1 mongod mongod 231214 Oct 5 08:52 mongod.log
-rw-r----- 1 mongod mongod 0 Sep 30 00:00 mongod.log.2023-09-30T00-00-01
-rw------- 1 mongod mongod 33606 Oct 1 00:00 mongod.log-20231001_00.gz
-rw-r----- 1 mongod mongod 0 Oct 1 00:00 mongod.log.2023-10-01T00-00-01
-rw------- 1 mongod mongod 33562 Oct 2 00:00 mongod.log-20231002_00.gz
-rw-r----- 1 mongod mongod 0 Oct 2 00:00 mongod.log.2023-10-02T00-00-01
-rw------- 1 mongod mongod 37762 Oct 3 00:00 mongod.log-20231003_00.gz
-rw-r----- 1 mongod mongod 0 Oct 3 00:00 mongod.log.2023-10-03T00-00-01
-rw------- 1 mongod mongod 37930 Oct 4 00:00 mongod.log-20231004_00.gz
-rw-r----- 1 mongod mongod 626234 Oct 5 00:00 mongod.log-20231005_00

daily
rotate 5
dateext
dateformat -%Y%m%d_%H
compress
delaycompress
missingok
create640 mongod mongod
sharedscripts
postrotate
/bin/kill -USR1 cat /var/run/mongodb/mongod.pid 2>/dev/null >/dev/null 2>&1
endscript