I need to extract information from a log file that is deleted and recreated every time a program runs. After detecting that the file exists (again), I would like to tail
it for a certain regexp.
The regexp will be matched a few times, but the result is always the same and I want to print it just once and after that go back to monitoring when the file is re-created.
I looked at ways of detecting file creation. One way would be via inotifywait
, but that requires installing a separate package.
Perhaps a simpler way is to take advantage that tail prints to stderr
when a file that is being tailed is deleted and created:
tail: '/path/to/debug.log' has become inaccessible: No such file or directory
tail: '/path/to/debug.log' has appeared; following new file
So I applied this solution which is working:
debug_file="/path/to/debug.log"
while true; do
# Monitor the log file until the 'new file' message appears
( tail -F $debug_file 2>&1 & ) | grep -q "has appeared; following new file"
# After the new file message appears, switch to monitoring for the regexp
tail -F "$debug_file" | while read -r line; do
id=$(echo "$line" | sed -n 's/.* etc \([0-9]\+\),.*/\1/p')
if [ -n "$id" ]; then
echo "ID: $id"
break # Exit the inner loop after the first match
fi
done
done
But I don't like that this solution starts 2 different tail
processes. Is there a way to achieve the same result, but using just 1 tail
process?
And then switch 'modes', start by looking for file creation, then look for the regexp and once that is found go back to 'standby' mode waiting for the log file to be deleted and created again.
Is inotifywait a more elegant solution? Ideally I would like a solution I could port easily to Windows CMD.
tail
here? You're not watching the output, you seem to only care about a single value, so why bringtail
in at all? Why not justgrep
the file for your pattern? – terdon Mar 19 '24 at 19:42tail
be needed over just runninggrep
? Why is it a good thing to keep the process active? – terdon Mar 19 '24 at 19:57tail -F FILE
? Works even for files which not even exists (yet). – paladin Mar 20 '24 at 00:37--follow=name
option ? – Archemar Mar 20 '24 at 12:40