I'm trying to write a command to test that data is written to a file. My first approach was:
- Start reading in the background.
- Write some data to the file.
- Wait for the reader to find a result.
- Repeat indefinitely to see if there's a timing issue.
In script form:
while true
do
grep -q foo <(tail -n0 -f /var/log/syslog) &
logger foo && logger line && wait
done
(the logger line
command is to avoid last message repeated N times
lines in the file)
This version will typically loop a once or twice before getting stuck at the wait
command, so it looks like tail
didn't have time to start reading before logger foo
had written to the file.
What is the best way to guarantee that tail
is reading before continuing? These workarounds are not ideal:
Pause before
logger
(won't work in the case of slow file systems)sleep 1 && logger foo && logger line && wait
Start a second reader, and assume that the first one has started reading by the time the second has been shown to. This looped a few thousand times before getting stuck:
grep -q foo <(tail -n0 -f /var/log/syslog) & grep -q bar <(tail -n0 -f /var/log/syslog) & while kill -0 $! do logger bar logger line done logger foo && logger line && wait
stat
to see if the size has changed. More bytes means more data. So your initial premise is solved very easily. This looks like a classic X/Y problem.logger
) is writing to a world-writable log file. Sostat
is not the solution.