0

Please help me. I have a log files, for example: test_2014_03_13.log

i need tail it with grep and when created new file (test_2014_03_14.log) i need switch tailing with grep to a new file without skipping lines.

i need info about switching without skipping data.

Sorry for my bad english.

1 Answers1

0

As far as I understand you. I think you want to change the log file when a new one is created.

You need to run two different processes.

  • The first one to search for changes in the directory so that you know when the new file was created, and then change it.
  • The second process would be in charge of executing the "Tail+Grep", and when it is closed with the Kill instruction it should continue with the most recent file.

This is an example in Bash that you can easily write in Python. I read too late that you were asking for an example in Python.

#!/bin/bash

pidTail=0

inotifywait -m /var/log/yourfiles -e created |
while read path action file; do 
    echo “changed to $file”
    if [ ! $pidTail -eq 0 ]; then
        kill $pidTail
    fi
    tail -f $file | grep something >> newfile.log & 
    pidTail=$!
done

Note: I've written it right here and I haven't tested it, it may contain typos ;)

Even better late than never!

Sources:

Threads in bash?

Script to monitor folder for new files?

ucotta
  • 557
  • 4
  • 7