0

i wonder if there is a more simplyfied way to run the tail -f or -F on a logfile and execute a command each time a special keyword is mentioned.

this is my working solution so far, but i don't like it for following reasons:

  • i have to write new lines for each match to log file to avoid endless loop
  • tail does not follow exactly the log, it could miss some lines while the command is executed
  • i am not aware about CPU usage because of high frequency

example:

#!/sbin/sh
while [ 1 ]
  do
    tail -n1 logfile.log | grep "some triggering text" && mount -v $stuff >> logfile.log
done

i tried the following but grep won't give return code until the pipe break

#!/sbin/sh
tail -f -n1 logfile.log | grep "some triggering text" && mount $stuff

i am running a script on android which is limited to
busybox ash

edit:
the problem is related to grep. grep won't give return code until the last line. what i need is return code for each line. maybe kind of a --follow option for grep, or sed, awk, or a user defined function which works with tail --follow

alecxs
  • 701
  • 8
  • 17

0 Answers0