0

This is a script in bash. Here

Which capture a string "ERROR: your TCP- connection is dead." from a Live dynamic log file. Here the name of a log file "TcpRcpt.log" which contain sample of a log data below.


SAT Mar 26 19:55:37  2016 TCPRcpt-0297--ERROR: your TCP- connection is dead.
SAT Mar 26 19:55:37  2016 TCPRcpt-0297--RUNNING
SAT Mar 26 19:55:37  2016 TCPRcpt-0297--RUNNING
SAT Mar 26 19:55:37  2016 TCPRcpt-0298--ERROR: your TCP- connection is dead.

Now, issue step logtail TcpRcpt.log | grep -m 1 "ERROR: your TCP- connection is dead." | sed 's/.*TCPRcpt-/ PID /;s/ -//' >> LOGFILE.LOG

It always gives 0 value, even though it does not capture the string. Which makes if condition to send notification every time, when loop execute.

Now my question: Is there any other alternative? which notify via email when it capture the string every time?

  • grep m 1 "ERROR: your TCP connection is dead." looks for the string m in the files 1 and "ERROR: your TCP connection is dead.". for starters. – DopeGhoti Mar 27 '16 at 17:21
  • Sorry it's -m there – Bipul kumar Mar 27 '16 at 17:23
  • I tried to fix your formatting, but in doing so I noticed that the log has "TCP-" while your grep does not have a dash – Jeff Schaller Mar 27 '16 at 17:31
  • Also, the capitalization of tcprcpt is different between the log file and the sed – Jeff Schaller Mar 27 '16 at 17:33
  • Yes agree. between it was my mistake, I have corrected it. now check. – Bipul kumar Mar 27 '16 at 17:38
1

'It always gives 0 value' because that's the exit code of the last command in the pipeline - sed, which succeed (returns 0) even when no replace happens.

Setting pipefail option will make the result of the pipe set to the result of the first failed command in the pipeline, if any. Modify your script by setting that option before calling logtail :

    ...
    set -o pipefail
    logtail TcpRcpt.log | grep -m 1 "ERROR: your TCP- connection is dead." | sed 's/.*TCPRcpt-/ PID /;s/ -//' >> LOGFILE.LOG
    ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.