$RodHat_/search
Console Tips

FreeBSD newsyslog(8): log rotation you can dry-run and actually read

Published by

This article may contain Amazon affiliate links. I may earn a commission if you buy through them. Disclosure.

Editorial card: FreeBSD newsyslog(8): log rotation you can dry-run and actually read
Editorial card: RodHat
On this page · 6 sections
  1. The format
  2. Flags worth knowing
  3. The part that bites: who gets signaled
  4. Test before you trust it
  5. Drop-ins instead of editing the base file
  6. The cons

Log rotation is the chore everybody configures once, forgets, and rediscovers when /var fills or a daemon keeps writing into a file nobody can see. FreeBSD ships newsyslog(8) in base. It is one binary, one config file with columns, and it does what it says. No DSL, no postrotate shell heredoc to get wrong at 3am.

It runs from /etc/crontab once an hour, as newsyslog, and decides per file whether that hour is the moment to rotate. If you never read what periodic(8) runs on your behalf, read it too; the same “stuff cron does that you never looked at” applies here.

The format

Each line of /etc/newsyslog.conf is whitespace-separated columns:

# logfilename          [owner:group]  mode count size when  flags [pid_file] [sig_num]
/var/log/messages                     644  5     100  *     JC
/var/log/myapp/app.log www:www        640  14    *    $D0   JC   /var/run/myapp.pid  USR1
  • count is how many archives to keep. Rotated files are numbered, newest at .0, so app.log.0.bz2 is yesterday’s and the highest number gets deleted.
  • size is in kilobytes. * means ignore size.
  • when is a time spec. * means ignore time, $D0 means daily at midnight, $W0D5 is weekly on a chosen weekday, $M1D0 is the first of the month. An @ prefix takes an ISO 8601 timestamp for a one-off.
  • If you give both size and when, the file rotates when either trips. People expect “and”. It is “or”.

Flags worth knowing

FlagWhat it does
CCreate the log file if it does not exist
J / X / ZCompress archives with bzip2 / xz / gzip
NDo not signal any process after rotating
BBinary file, do not write the “log file turned over” trailer line
GThe filename is a glob

B matters more than it sounds. Point newsyslog at a binary log such as a wtmp-style file without it and you append a text line into the middle of a format that does not expect one.

The part that bites: who gets signaled

After renaming the old file and creating a new one, newsyslog has to tell the writer to reopen its log. If the writer keeps its descriptor to the old inode, everything lands in app.log.0 forever and the fresh app.log stays empty. I covered the reader-side version of that mess in the tail -f tale; this is the writer side.

Without a pid_file column, newsyslog signals syslogd using /var/run/syslog.pid and sends SIGHUP. That is correct for anything logging through syslog and wrong for everything else. For your own daemon, name its pid file and, if it reopens on something other than HUP, the signal:

/var/log/myapp/app.log www:www 640 14 * $D0 JC /var/run/myapp.pid USR1

Use N when the program does not need a signal at all, for instance because it opens, appends, and closes on every write, or because you are rotating a file nothing holds open. A daemon that has no reopen signal and keeps the fd is the one case newsyslog cannot save; it needs a restart, or it needs fixing.

Test before you trust it

Never write a rule and wait an hour. Ask newsyslog what it would do:

newsyslog -nv

-n means do nothing, -v narrates. It prints each log it considers and why it will or will not rotate, including the size and age it measured. Restrict it to one file by naming it:

newsyslog -nv /var/log/myapp/app.log

To force a rotation of that file right now, once you trust the line:

newsyslog -vF /var/log/myapp/app.log

If you want to see what the signal step does, ktrace and kdump the daemon during a forced rotation and look for the signal arriving and the openat that follows. A daemon that gets the signal and never reopens is a bug you want to find on a Tuesday, not during an incident.

Drop-ins instead of editing the base file

Do not edit /etc/newsyslog.conf for your own services. The file includes /etc/newsyslog.conf.d/ and /usr/local/etc/newsyslog.conf.d/, so a package or you can ship /usr/local/etc/newsyslog.conf.d/myapp.conf containing just your line. Base upgrades then do not fight your edits, and grep -r myapp /etc/newsyslog.conf.d /usr/local/etc/newsyslog.conf.d tells you who owns a rule.

The cons

  • It runs hourly. A 100 KB size trigger on a chatty file will overshoot while you wait for the next run. For a fast-growing log, fix the logging rate, or cap it at the source.
  • It is rename-and-signal. It cannot rescue a program that never reopens its log, and there is no copytruncate equivalent to fall back on in the way Linux admins expect; truncating under a writer is a different, lossier trade.
  • Compression runs during the rotation, so a very large file on a slow disk makes the job take a while. Check the W flag in newsyslog.conf(5) if that matters to you.
  • Wrong ownership in column two is silent until the daemon, now running as an unprivileged user, cannot write to the new file. Test with -nv, then force one rotation and look at ls -l.

If you are on Linux, the shape is the same under a different name: rotate, signal, verify the writer reopened. Check the descriptor, not the config; ls -l /proc/<pid>/fd will tell you which inode it is holding in half a second.