Shell function to tail a log file for a specific string for a specific time

后端 未结 5 1330
情话喂你
情话喂你 2020-12-29 08:39

I need to the following things to make sure my application server is

  1. Tail a log file for a specific string
  2. Remain blocked until that string is printe
5条回答
  •  不思量自难忘°
    2020-12-29 09:02

    You can use signal handlers from shell scripts (see http://www.ibm.com/developerworks/aix/library/au-usingtraps/index.html).

    Basically, you'd define a function to be called on, say, signal 17, then put a sub-script in the background that will send that signal at some later time:

    timeout(pid) {
       sleep 1200
       kill -SIGUSR1 $pid
    }
    
    watch_for_input() {
       tail -f file | grep item
    }
    
    trap 'echo "Not found"; exit' SIGUSR1
    timeout($$) &
    watch_for_input
    

    Then if you reach 1200 seconds, your function is called and you can choose what to do (like signal your tail/grep combo that is watching for your pattern in order to kill it)

提交回复
热议问题