LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-08-2018, 03:26 AM   #1
Vignesh Radhakrishnan
LQ Newbie
 
Registered: Nov 2018
Posts: 1

Rep: Reputation: Disabled
Monitor the logs Continuously and triggers the output if keyword matches and continue the monitoring


Hi All,

I want to monitor the logs continuously and sends a error if keyword/pattern matches and continues the monitoring

I tried below script

tail -F /data/log/test.log |

egrep -io 'got signal 15 | now exiting' |

while read -r line ; do

case "$line" in
"got signal 15")
echo "hi"
;;
"now exiting")
echo "hi2"
;;
*)


esac
done

The problem is tail is not working here , whenever the new log details added its not going to the case statement and echos the output

I could get the output if i use cat/less/more

Could you someone please tell what mistake i have done here ?


Thanks in advance
 
Old 11-08-2018, 06:47 AM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Welcome to LQ!
I have no idea why your code isn't working.
If tail -{f,F} is interactive and defaults to 10 lines...
I'd try
Code:
tail -FN0...
But I found a link that may help you onto the path to a solution.
Monitor Log File for Pattern Match and Trigger Action
Looks to be thorough, however they are going an "extra step" by killing tail "when it dies".
Why use it if you have to kill it? (asking the first link)

cat/less/no more all day long.
Is this a bash script?
Are the logs multiline like tomcat/java?

Monitoring a file until a string is found
and yet another: Shellscript to monitor a log file if keyword triggers then execute a command?

Have fun!
 
1 members found this post helpful.
Old 11-08-2018, 07:04 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by Vignesh Radhakrishnan View Post
Hi All,
I want to monitor the logs continuously and sends a error if keyword/pattern matches and continues the monitoringI tried below script
Code:
tail -F /data/log/test.log |

egrep -io 'got signal 15 | now exiting' |

while read -r line ; do

case "$line" in
   "got signal 15")
    echo "hi"
        ;;
    "now exiting")
    echo "hi2"
         ;;
    *)
esac
done
The problem is tail is not working here , whenever the new log details added its not going to the case statement and echos the output I could get the output if i use cat/less/more Could you someone please tell what mistake i have done here ?
The tail issue that habitual pointed out would be a good start, but using logwatch or another such utility would be the best bet here. It's written/designed specifically to look at log files for patterns. Also, do you have any systems monitoring in place now? Because things like zabbix/nagios can ALSO be used to watch log files, and act accordingly.

There are also LOTS of already-written shell scripts to watch a log..you can find these with a brief Google search. I'd suspect there's an issue with your case statements/code there, but given the many options that already exist to do this, I'd use those.
 
1 members found this post helpful.
Old 11-14-2018, 03:08 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,781

Rep: Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199
I would skip the grep and search the string anywhere in the line i.e. *string*
Code:
tail -F /data/log/test.log |
while read -r line
do
  case "$line" in
  *"got signal 15"*)
    echo "hi"
  ;;
  *"now exiting"*)
    echo "hi2"
  ;;
  *)
  esac
done
 
Old 11-14-2018, 10:55 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You might want to consider using a Perl module designed to do just that https://metacpan.org/pod/File::Tail
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
If output from tail returns keyword > print 'X' Warriorsofthenight Programming 7 02-12-2018 07:02 AM
Edit and direct continuously output from program (rsstail) iwtbf Linux - Newbie 3 06-29-2015 05:56 AM
using grep to output non-matches into output file binny959 Linux - General 5 12-24-2010 11:20 PM
need a script to continuously monitor appended data to a log file akitty Programming 5 10-14-2010 02:39 AM
monitor continuously turns off and on epihammer Mandriva 1 05-21-2004 05:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration