Log File Zimbra Script: 3 Easy Fixes, Avoid Lost Lines

A log file zimbra script that reads only the new lines of /var/log/zimbra.log since its last run is useful for alerts and reports. The original post of this blog showed two Bash functions for that and no way to run them. This rewrite builds the missing main part, tests the functions with a sample Postfix-style log in a Debian 13 container, and shows that the old script loses lines in two situations: when the log is rotated and when lines arrive during a run. A corrected POSIX script follows.

Scope: there is no Zimbra server in the lab, so the log is a sample file with five lines in the usual syslog format. The Zimbra Administrator Guide (read on 2026-11-01) says that /var/log/zimbra.log is the Zimbra syslog: it holds the activity of the MTA (Postfix, amavisd, anti-spam, anti-virus), the Logger, authentication (cyrus-sasl) and directory (OpenLDAP) components. The logic of the script does not depend on Zimbra.

How the old log file zimbra script works

The old code keeps the number of lines already read in number.txt. The first function, f_check_folder, creates the working folder and writes 0 into that file. The second, f_get_log, reads the number, runs tail -n +"$SUM" to copy the lines after it into a temporary file, and then writes the current line count of the log into number.txt. Because the post never called the functions, the test script adds two lines at the end: f_check_folder and f_get_log.

On the normal path the log file zimbra script works. With a five-line log, the first run reported new lines ... : 5 and stored 5. After three lines were appended, the second run reported 3 and showed exactly those three, and the number became 8. A third run, with nothing new, reported 0. If you only need the normal path, the code of this log file zimbra script is fine.

Bugs in the log file zimbra script

The first bug is the shell. The script uses [[ ... ]], which dash does not have. Run with sh, it printed old.sh: 8: [[: not found and continued, because the failed test fell into the else branch, so it only worked by accident. Use #!/bin/bash or the POSIX [ ... ].

The second bug of the log file zimbra script is log rotation. Logs like zimbra.log are rotated; see the logrotate manual. In the test the log was moved away and a new file received two lines while number.txt still said 8. The script ran tail -n +9 on a file with two lines, read 0 lines, and then stored 2. Those two new lines were never processed. The script stays blind until the new file grows beyond the old count.

The third bug is a race. The script reads the log twice, once with tail and once with wc -l to save the position. Lines that arrive between the two commands are counted as read although they were not. In the test a two-second pause was placed between the two commands and two lines were added during it: the file had 8 lines, number.txt said 8, and the next run read 0 lines. Lines 7 and 8 were skipped for good. On a busy mail server, with many lines per second, this happens all the time.

Log file zimbra script test: the old script reads 0 new lines after rotation and after a race, the fixed script reads them
The old log file zimbra script loses lines after a rotation and during a race; the fixed script keeps them.

A corrected log file zimbra script

The fix keeps the idea but stores the inode of the log together with the number of lines that were really read, and counts the lines from the temporary file, not from a second read of the log:

#!/bin/sh
LOG=${LOG:-/var/log/zimbra.log}
STATE_DIR=${STATE_DIR:-/var/tmp/zimbra-logread}
OUT=$STATE_DIR/new_lines.log
STATE=$STATE_DIR/state

mkdir -p "$STATE_DIR" || exit 1
[ -r "$LOG" ] || { echo "cannot read $LOG" >&2; exit 1; }

read -r old_inode old_lines 2>/dev/null < "$STATE" || { old_inode=0; old_lines=0; }
inode=$(stat -c %i "$LOG")
total=$(wc -l < "$LOG")

if [ "$inode" != "$old_inode" ] || [ "$total" -lt "$old_lines" ]; then
  old_lines=0
fi

tail -n +"$((old_lines + 1))" "$LOG" > "$OUT"
read_lines=$(wc -l < "$OUT")
echo "$inode $((old_lines + read_lines))" > "$STATE.tmp" && mv "$STATE.tmp" "$STATE"
echo "new lines: $read_lines"

Results in the lab: the first run read all 5 lines, the second read the one new line, the third read 0. After mv of the log and a new file with two lines (a new inode) it read both; after the file was emptied with : > $LOG it read the one line written afterwards, because the count was lower than before. In the race test, where two lines were appended after tail had finished, the saved state counted only the lines actually read, so the next run delivered both late lines. A missing log gave cannot read /nonexistent and exit code 1.

Limits and common mistakes of the log file zimbra script

  • The script cannot tell a log that was rotated and then grew past the old size within one interval, if the new file also has the same inode; with logrotate using copytruncate this can happen. Run it often enough that it does not, and test your own logrotate settings.
  • Do not store the state in /tmp on a host that clears it at boot; the lab used /var/tmp.
  • Two copies of the script at the same time can overwrite each other's state. Run it from one cron entry.
  • Check the exit code when you use it in a cron job, and write the new lines somewhere that the next step of your report reads before the next run replaces the file.
  • Remember that the output file is overwritten each time; copy it if you need a history of what was read.

If a Zimbra service is down and you read the log to find out why, see Zimbra some services are not running.

FAQ

Can I use logtail instead of this log file zimbra script? Tools like logtail do the same job; they were not tested here.

Does this log file zimbra script work with the real Zimbra log? The logic was tested on a sample log only. Try it on a copy of /var/log/zimbra.log first.

Comments

Post a Comment

Popular posts from this blog

How to Play Minecraft Bedrock Edition on Linux: A Comprehensive Guide for Tech Professionals

Linux desktop frozen? My 5 go-to tricks to try - before forcing a hard reboot

The Ultimate Guide: How to Set Up DXVK in Wine on Linux for Enhanced Gaming Performance