MongoDB server keeps deleted log files open after rotation

Hi all,

I made a script to rotate the logs every day, but my log files remain open after the rotation (see attachment).

Even if I restart the mongod processes, this problem will appear in the next few days.

Is there a problem with my script? Or the “kill -SIGUSR1 ” command cannot be used when MongoDB is balancing?

Any suggestions or root cause guessing?

Thanks~

$ crontab -l

05 00 * * * /opt/log_bk.sh

$ cat /opt/log_bk.sh

#!/bin/sh
/bin/kill -SIGUSR1 cat /opt/foo/conf/SB/SB11.pid 2> /dev/null 2> /dev/null || true
mv /opt/foo/logs/SB/SB11.log.* /opt/foo/logsbk/SB
compress /opt/foo/logsbk/SB/SB11.log.*

/bin/kill -SIGUSR1 cat /opt/foo/conf/SB/SB31.pid 2> /dev/null 2> /dev/null || true
mv /opt/foo/logs/SB/SB31.log.* /opt/foo/logsbk/SB
compress /opt/foo/logsbk/SB/SB31.log.*

find /opt/foo/logsbk/SB/ -type f -name “*.Z” -mtime +7 -exec rm -rf {} ;

You need to SIGKILL after moving the file. Don’t roll your own just use logrotate

here is my file for logrotate.

# /etc/logrotate.d/mongod
/var/log/mongodb/mongod.log {
	rotate 36500
	compress
	daily
	postrotate
		systemctl kill -s USR1 mongod
	endscript
}

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.