§logrotate

  1. See if it installed

root@hc4Jammy:/var/log# /sbin/logrotate --version logrotate 3.19.0

Default mail command:       /usr/bin/mail
Default compress command:   /bin/gzip
Default uncompress command: /bin/gunzip
Default compress extension: .gz
Default state file path:    /var/lib/logrotate/status
ACL support:                yes
SELinux support:            yes

$ logrotate <-- else


2. Create a logrotate configuration file specifically for MongoDB. You can create a new configuration file, for example, /etc/logrotate.d/mongodb:

-@hc4Jammy.yushei.com.tw 

logrotate /var/log/mongoDB-*.log

/var/log/mongoDB-*.log { daily rotate 7 compress delaycompress missingok notifempty create 0640 syslog sudo sharedscripts # su: This stands for "switch user.root:sudo
su root syslog

postrotate
    #    /usr/bin/killall -SIGUSR1 mongod
     /usr/bin/systemctl restart syslog-ng.service >/dev/null 2>&1 || true
endscript

}

or in n2Bookworm.yushei.com.tw

logrotate /var/log/mongodb/mongod.log

/var/log/mongodb/mongod.log { daily rotate 7 compress delaycompress missingok notifempty create 0640 syslog sudo sharedscripts # su: This stands for "switch user.root:sudo
su root syslog

# postrotate
# e.g. /usr/bin/killall -SIGUSR1 mongod
#

}

  1. testing sudo logrotate -d
root@hc4Jammy:/var/log# logrotate -d /etc/logrotate.d/mongodb
WARNING: logrotate in debug mode does nothing except printing debug messages!  Consider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.d/mongodb
Reading state from file: /var/lib/logrotate/status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state

Handling 1 logs

rotating pattern: /var/log/mongoDB-*.log  after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
switching euid from 0 to 0 and egid from 0 to 111 (pid 6646)
considering log /var/log/mongoDB-odroid-01.log
Creating new state
  Now: 2024-05-29 15:33
  Last rotated at 2024-05-29 15:00
  log does not need rotating (log has already been rotated)
not running postrotate script, since no logs were rotated
switching euid from 0 to 0 and egid from 111 to 0 (pid 6646)

root@hc4Jammy:/var/log# ls -l /var/log/mong*
-rw------- 1 root    root     33096 May 29 15:32 /var/log/mongoDB-odroid-01.log
-rw-r--r-- 1 mongodb mongodb 217221 Jul 24  2023 /var/log/mongod.log
  1. make it auto
root@hc4Jammy:/var/log# sudo logrotate -f /etc/logrotate.d/mongodb
root@hc4Jammy:/var/log# ls -l /var/log/mongoDB-odroid-01.log*
-rw-r----- 1 syslog sudo     0 May 29 15:38 /var/log/mongoDB-odroid-01.log
-rw------- 1 root   root 35422 May 29 15:35 /var/log/mongoDB-odroid-01.log.1

after logrotate -f then it will do it automatically everyday