The Missing Bit

Watching a file under OpenBSD

openbsd sysadmin

I wanted to watch a file under OpenBSD to synchronize my PowerDNS database between two hosts.

First, I tried the entr tool, while it worked, it would usually die after a few days.

OpenBSD philosophy being that daemon should not be restarted on crash, there is no clean way to restart it if it crashes.

I ended up writing a simple shell script. But as I did, I found a few small gotcha and I think it can make it to the posterity.

Below a fully commented version:


# This is the file I am observing

# This is the command I run if the file changes, simply rsync wrapper

# This is this script, to let me invoke it recursively

# If we are invoked with the watch argument, let's loop forever
if [ "$1" = "watch" ] ; then

  # Get the file modified time (OpenBSD format)
  TIME0=$(stat -f  %m $DB)

  while true
    # Get the time again
    TIME1=$(stat -f  %m $DB)

    # If it changed simply run the command and save the time
    if [[ "$TIME0" != "$TIME1" ]]
    # This is the poll time, you can make it shorter, but 10 second is enough
    # This part is important, we fork the sleep call to the background, this
    # is to ensure that this script will exit immedialy when killed with
    sleep 10 &
    wait $!

# If not invoked with the watch argument, invoke again with it in the background
# This acts as a start script
if [ "$1" != "watch" ] ; then
  $SELF watch &
  exit 0
If you wish to comment or discuss this post, just mention me on Bluesky or email me.