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?