On a recently installed MongoDB 3.x server, it seems that MongoDB now writes much more logs than the previous 2.x version.
According to the documentation, a log rotation, done by MongoDB itself, can be forced by sending the SIGUSR1 signal to the MongoDB process. When this is done, a rotated log file is created with a date extension:
root@mongoserver:/var/log/mongodb# ls -ltr
-rw-r--r-- 1 mongodb nogroup 168167263 Jul 6 10:31 mongod.log.2015-07-06T08-31-32
-rw-r--r-- 1 mongodb nogroup 1371461 Jul 6 10:32 mongod.log
My goal was to use send SIGUSR1 as a prescript command in logrotate and then compress the newly created file (mongod.log.2015-07-06T08-31-32 in this case). Unfortunately this doesn't work with logrotate, because the file extension, created by MongoDB, is too variable.
So I quickly created the following script which sends SIGUSR1 to the MongoDB PID, then compresses the rotated logfile:
#!/bin/bash
#####################################################
# MongoDB Logrotate Script
# (c) Claudio Kuenzler www.claudiokuenzler.com
# 20150706 ck Created script
#####################################################
# Variables
logpath=/var/log/mongodb
pidfile=/run/mongodb.pid
#####################################################
# Start
(
test -f $logpath/mongod.log || exit 1
echo "Starting MongoDB log rotation"
echo "Current logfile:"
ls -la $logpath/mongod.log
echo "Launching SIGUSR1 command"
kill -SIGUSR1 `cat $pidfile`
echo "Compressing new logfile"
find $logpath/ -name "mongod.log.$(date +%Y)*" ! -name "*.gz" -exec gzip {} +
echo "Finished MongoDB log rotation"
echo "-----------------------------------------------------"
) 2>&1 | tee -a ${logpath}/rotate.log
exit 0
This script is now run once a day by as a cronjob.
Successfully tested on Ubuntu 14.04 LTS with MongoDB 3.0.4 (installed from 10gen repo).
Side note: It's of course also possible to add a deletion of old rotated logfiles into the script, for example:
find $logpath/ -name "mongod.log.*gz" -mtime +7 -exec rm {} +
ck from Switzerland wrote on Aug 6th, 2019:
nemali, try to run the script as root or with sudo because of the kill command.
> "And also need your help on if you add script for generating log for 31 days and after 31 days old logs should get delete automatically"
Sorry, but the script is as is and I currently do not have time to get into this. You can adjust it as you wish. If you want professional (paid) support, I can investigate and adjust the script for your environment. Just contact me via e-mail if you're interested.
nemali from wrote on Aug 6th, 2019:
Hello CK,
I tried to run your script with adjusted variables. I schedule cron for every minute, it was appending log of every minute rather than with date extension. When i tried to run manually through script it gives following error.
sh logrotate.sh
Starting MongoDB log rotation
Current logfile:
-rwx------. 1 nemali1155 nemali1155 3187 Aug 6 01:11 /home/nemali1155/shard/log/mongo.log
Launching SIGUSR1 command
logrotate.sh: line 18: kill: SIGUSR1: invalid signal specification
Compressing new logfile
Finished MongoDB log rotation
-----------------------------------------------------
Could you please verify the following adjusted are good to go or need to midify. And also need your help on
if you add script for generating log for 31 days and after 31 days old logs should get delete automatically
============================
#!/bin/bash
#####################################################
# MongoDB Logrotate Script
# (c) Claudio Kuenzler www.claudiokuenzler.com
# 20150706 ck Created script
#####################################################
# Variables
logpath=/home/nemali1155/shard/log
pidfile=/home/nemali1155/shard/pid/mongo.pid
#####################################################
# Start
(
test -f $logpath/mongo.log || exit 1
echo "Starting MongoDB log rotation"
echo "Current logfile:"
ls -la $logpath/mongo.log
echo "Launching SIGUSR1 command"
kill -SIGUSR1 `cat $pidfile`
echo "Compressing new logfile"
find $logpath/ -name "mongo.log.$(date +%Y)*" ! -name "*.gz" -exec gzip {} +
echo "Finished MongoDB log rotation"
echo "-----------------------------------------------------"
) 2>&1 | tee -a ${logpath}/mongo.log
exit 0
ck from Switzerland wrote on Aug 5th, 2019:
Nemali, what exactly do you mean by "but it throws following error"? Your output does not contain an error, just the script with adjusted paths for your environment. Make sure you download the script (https://raw.githubusercontent.com/Napsty/scripts/master/mongodb/logrotate-mongodb.sh), adjust the variables and set the execution permissions (e.g. chmod 755) on it. Also make sure that you execute the script as either the mongodb user (under which the mongodb processes run) or as root.
Nemali from wrote on Aug 5th, 2019:
i tried to execute this file, but it throws following error. i am not sure about shell scripts.
I tried to edit your file but didnt worked. We are using MongoDB Community version, Can you please me on same..
# current path for pid and log as mentioned
#!/bin/bash
#####################################################
# MongoDB Logrotate Script
# (c) Claudio Kuenzler www.claudiokuenzler.com
# 20150706 ck Created script
#####################################################
# Variables
logpath=/home/nemali1155/shard/log/mongo.log
pidfile=/home/nemali1155/shard/pid/mongo.pid
#####################################################
# Start
(
test -f $logpath/home/nemali1155/shard/log/mongo.log || exit 1
echo "Starting MongoDB log rotation"
echo "Current logfile:"
ls -la $logpath/home/nemali1155/shard/log/mongo.log
echo "Launching SIGUSR1 command"
kill -SIGUSR1 `cat $pidfile`
echo "Compressing new logfile"
find $logpath/ -name "mongo.log.$(date +%Y)*" ! -name "*.gz" -exec gzip {} +
echo "Finished MongoDB log rotation"
echo "-----------------------------------------------------"
) 2>&1 | #tee -a ${logpath}/rotate.log
exit 0
AWS Android Ansible Apache Apple Atlassian BSD Backup Bash Bluecoat CMS Chef Cloud Coding Consul Containers CouchDB DB DNS Database Databases Docker ELK Elasticsearch Filebeat FreeBSD Galera Git GlusterFS Grafana Graphics HAProxy HTML Hacks Hardware Icinga Influx Internet Java KVM Kibana Kodi Kubernetes LVM LXC Linux Logstash Mac Macintosh Mail MariaDB Minio MongoDB Monitoring Multimedia MySQL NFS Nagios Network Nginx OSSEC OTRS Observability Office OpenSearch PGSQL PHP Perl Personal PostgreSQL Postgres PowerDNS Proxmox Proxy Python Rancher Rant Redis Roundcube SSL Samba Seafile Security Shell SmartOS Solaris Surveillance Systemd TLS Tomcat Ubuntu Unix VMWare VMware Varnish Virtualization Windows Wireless Wordpress Wyse ZFS Zoneminder