Sample Header Ad - 728x90

How to tail continuously a log file that is being deleted and recreated?

1 vote
3 answers
310 views
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.
Asked by user2066480 (173 rep)
Mar 19, 2024, 05:27 PM
Last activity: Mar 20, 2024, 12:16 PM