follow logfile with tail and exec on event

五迷三道 提交于 2021-01-29 05:01:38

问题


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

来源:https://stackoverflow.com/questions/56675613/follow-logfile-with-tail-and-exec-on-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!