§logrotate
- 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
#
}
- daily: Rotates the log file daily.
- rotate 7: Keeps up to 7 rotated log files.
- compress: Compresses rotated log files.
- delaycompress: Postpones compression of the previous log file until the next rotation cycle.
- missingok: Ignores missing log files.
- notifempty: Doesn't rotate the log file if it's empty.
- create: Specifies the permissions and ownership for the newly created log files.
- sharedscripts: Runs the postrotate script only once for all matching log files.
- ?? postrotate: Executes the specified command after rotation. In this case, it sends the SIGUSR1 signal to the mongod process to reopen log files.
- Adjust permissions: Ensure that the user/group specified in the create directive (in this example, mongodb) has write permissions to the log directory.
- 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
- 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