I'm trying to write a script that uses inotifywatch
to watch for changes in a log file. If a specific message is written to the log file, it's supposed to trigger a certain function. The script currently exists in this basic form:
while inotifywait -e modify /var/log/auth.log
do
alert=$(tail -n1 /var/log/auth.log | grep -E -o ".{0,7}password")
if [[ $alert == "Failed password" ]]
then
echo "FAILURE" >> test.log
elif [[ $alert == "cepted password" ]]
then
echo "LOGIN" >> test.log
fi
done
Everything works fine – up until the point where the logfile watched by inotifywatch
is rotated. Then it ceases to function. I presume that's because during rotation the watched file is renamed and no longer written to afterwards and a new file with the old name is created in its place which inotify
has never been told to watch in the first place.
I tried to circumvent this by switching from inotifywatch
to using tail -f
but the same problem seems to apply there as well.
Now I realize this could probably be solved by creating a huge if
-structure in which inotifywatch
not only watches for modify
, but also for file creation and restarts a watch for modification. But I like to keep things simple, so does anyone know if there's a simpler way? (And please, no, I don't want to use prefabricated solutions like fail2ban etc. – the interesting part for me is to create stuff like this myself with simple tools.)
-F
it should do what you want.