13

Mongo docs say that I can:

  1. use -SIGUSR1 signal and get the old log renamed and current switched
  2. use logrotate from OS

I want the OS's logrotate ability to zip old files and remove oldest, but see no way to tell mongod process to switch current log other than sending SIGUSR1.

So I wrote

/var/log/mongodb/*.log {
    daily
    rotate 5
    compress
    dateext
    missingok
    notifempty
    sharedscripts
    postrotate
        /usr/bin/killall -SIGUSR1 mongod
        /usr/bin/killall -SIGUSR1 mongos
    endscript
}

to /etc/logrotate.d/mongo.

And now get well-named logfiles from logrotate and empty logfiles like mongodb.log.2013-09-18T23-49-44 as traces of SIGUSR1 switching. How to get rid of the latter?

11

copytruncate works pretty well for logrotation.

a config similar to this should do the job for you:

/var/log/mongodb/*.log {
  daily
  missingok
  rotate 5
  compress
  dateext
  delaycompress
  copytruncate
  notifempty
}
  • 1
    This didn't work for me on RedHat 6.5. Logs were rotated but the original .log file continued to grow without bound. – Thomas Bratt Oct 16 '14 at 15:28
  • @ThomasBratt this is correct because without restarting mongo processes file handler remain open. fwics this method will not work all that well. – Mxx Oct 16 '14 at 15:31
  • @ThomasBratt take a look at this answer stackoverflow.com/a/8396266/949859 – Mxx Oct 16 '14 at 15:49
  • 1
    @Mxx Nice find - looks like copytruncate will work with the a postrotate step to signal to Mongo to truncate the logfile – Thomas Bratt Oct 16 '14 at 16:04
15

The server crashed for me if you send SIGUSR1 to mongod after you have moved the logfile out of the way with logrotate.

The following config is safe for the version I've tested - 2.6.6 on ubuntu 12.04 - the previous examples crashed the server. Put this into /etc/logrotate.d/mongod:

/var/log/mongodb/mongodb.log {
    weekly
    missingok
    rotate 4
    compress
    notifempty
    create
    postrotate
        /usr/bin/pkill -USR1 mongod
        rm /var/log/mongodb/mongodb.log.????-??-??T??-??-??
    endscript
}

See: https://jira.mongodb.org/browse/SERVER-11087 for more details and a suggestion from Akshay Kumar which I used in the above (use create instead of nocreate and cp /dev/null to the logfile).

In later releases there is supposed to be a logRotate option you can use to reopen the file - not rename it - which will work around the rename problem - but it didn't work in my version (it was unsupported).

See: https://github.com/mongodb/mongo/commit/616461d294bd9f5054ca38b302b6fc5d70fde20c

I've tested this with

logrotate -v -f /etc/logrotate.d/mongod
  • Works great for me in Mongo 2.6.11 on CentOS 7 – Tim Aug 30 '15 at 18:13
  • I had to replace mongodb.log with mongod.log and it works – cwhisperer Oct 15 '16 at 7:59
  • Can confirm that if systemLog.logRotate: reopen in mongod.conf, then the pkill will work as intended and no deletion of the renamed log file is required as none is created. – Julian H. Lam Mar 4 '17 at 21:52
15

Since mongodb 3.0 you can change the behavior of mongodb with the logRotate parameter, change in /etc/mongod.conf

systemLog:
  logAppend: true
  logRotate: reopen

See also Mongo Manuals.

Then you can use this logrotate configuration:

/var/log/mongodb/*.log {
    daily
    rotate 30
    size 50M
    compress
    dateext
    missingok
    notifempty
    sharedscripts
    postrotate
        /bin/kill -SIGUSR1 `cat /var/lib/mongodb/mongod.lock 2> /dev/null` 2> /dev/null || true
    endscript
}
  • A PID file created from the config file should probably be used.... See processManagement.pidFilePath in the config or the SystemD unit file's PIDFile setting (/var/run/mongodb/mongod.pid for me) – Gert van den Berg Apr 24 at 8:40
0

The following worked for me:

/var/log/mongo/mongod.log {
    missingok
    rotate 3
    size 100M
    nodateext
    postrotate
            /usr/bin/kill -USR1 $(cat /var/run/mongod.pid)
            rm -f /var/log/mongo/mongod.log.[0-9][0-9][0-9][0-9]-*
    endscript
}

Notes:

  • Tested on RedHat 6.5
  • The only way I could get a working solution was to delete the empty log files that Mongo produces
  • The location of the lock file depends on how MongoDB was installed
  • kill is a Bash builtin but logrotate runs under /bin/sh - which doesn't recognise SIGUSR1 on RedHat 6.5
  • I haven't tested with compress but it should be a straighforward addition
-1

Note that in version 3.0 and higher it is not required to kill your database daemon simply to rotate a log. Refer to documentation here:

https://docs.mongodb.com/manual/tutorial/rotate-log-files/

  • 1
    It seems Andrey already read that documentation; you're not really answering his question. – Law29 Jul 5 '16 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.