0

I have a logfile which I want to monitor and depending on which condition is met, different commands should be executed.

I found a solution that comes close to this here. Unfortunately it doesn't work for me and just gives me blank lines in the terminal.

xterm -e "$path_to_program | tee -a ${log_path}" & tail -f ${log_path} | awk '/Initialization Sequence Completed/ { system("echo "VPN is running."") } /[HOST_NOT_FOUND]/ { system("echo "error"") }

Are there other solutions that could help me with my problem?

  • It would be good to clarify why this method does not work for you, since it seems to fulfill the requirements of what you state that you want to do. – C G-K Aug 19 '15 at 14:47
  • I edited the part of my script that creates the log file. Should have done that before but I was too fixated on the awk command. My program to log into the VPN starts and writes things into the log file. At the same time my script should read the log and look for those patterns. Unfortunately I just get 30 blank lines and nothing more although "Initialization Sequence Completed" appears in the other terminal and in the log as well. – tigger92 Aug 19 '15 at 14:56

2 Answers2

1

You are not escaping characters correctly in the example code above: You need something more like:

tail -f ${log_path} | awk '/Initialization Sequence Completed/ { system("echo \"VPN is running.\"") } /\[HOST_NOT_FOUND\]/ { system("echo \"error\"")}'

You may also want to investigate the use of logrotate. If you make use of its postrotate rule, you could have a logs processor acting for you on a log that does not grow infinitely large and/or fill up storage space.

C G-K
  • 126
  • Thanks. Works like a charm. I needed the code for a little script and the log was just a trick for me so I can read what happens in the other terminal which gets opened when I start the VPN. (added that a little bit of code later to the script in my orignal post.) I think it will be enough for me if I clear the log file from time to time. – tigger92 Aug 19 '15 at 15:04
1

The solution before works but only with problems as I noticed. For some reason awk only reads the logfile every 2 minutes and then spams the terminal with "error" because in that time HOST_NOT_FOUND appeared several times in the log of course. I found a much nicer solution based on this thread. Just take in mind that tails has to be closed separately.

tail -f ${log_path} | while read LOGLINE
do 
     [[ "${LOGLINE}" == *"Initialization Sequence Completed"*]] && echo "VPN is running"
     [[ "${LOGLINE}" == *HOST_NOT_FOUND"* ]] && echo "VPN failed."
done