Register
It is currently Mon Sep 01, 2014 10:20 pm

tail output of log file and execute commands


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Wed Mar 19, 2008 4:42 pm   

Joined: Wed Mar 19, 2008 4:37 pm
Posts: 3
I've been googling and reading posts at this forum trying to learn some bash scripts. I'm a total noob at this and I'm at the point of giving up.

Therefor I seek your help.

Basically what I want to do is tail a log file, if the word "error" presents itself I want to issue a set of commands. And I want to run this script forever, meaning if the error presents itself, and the commands are executed I want it to start the tail for error again. (the log file will be deleted and recreated as part of the commands executed)

Please help


Top
 Profile  
 PostPosted: Thu Mar 27, 2008 2:41 am   

Joined: Sun Aug 19, 2007 12:04 pm
Posts: 13
Location: Texas
Here is the test to make it work :)
Code:
if [[ `tail ./pathtolog/thelog.log | grep -i "error"` != "" ]] #grep finds any instances of "error" in the tail'd input
then
     #whatever commands you wanted to run bc of this error
fi


make sure you start your script with "#!/bin/bash" or whatever shell you use (most likely bash ;)) and make the script executable before you run it ;)
Code:
chmod +x ./thescript.sh


happy scripting :)


Top
 Profile WWW  
 PostPosted: Thu Mar 27, 2008 3:56 am   

Joined: Wed Mar 19, 2008 4:37 pm
Posts: 3
Thank you for helping out.

It's not working quite as planned though, because I want it to watch the log file forever, not just check if the word error is in the file when I issue the script. I tried simply changing it to "tail -f", but that didn't work either. Maybe create a loop on the first line somehow ?


Top
 Profile  
 PostPosted: Thu Mar 27, 2008 9:15 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
The following may do what you're after, although it's not fantastic as continuously running process.

Code:
#!/bin/bash

while [ 1 ]
do
  if grep -i error ./path/to/logfile >/dev/null
  then
    # do stuff here
  fi
  sleep 15
done


This will scan the logfile looking for error every 15 seconds
It does scan the whole log file each time though so it may not be suitable if your logfile is large.


Top
 Profile  
 PostPosted: Thu Mar 27, 2008 10:33 am   

Joined: Wed Mar 19, 2008 4:37 pm
Posts: 3
Thank you, that one seems to do what I want it to.

but what do you mean by "although it's not fantastic as continuously running process."

the log file is very small, typically about 100bytes, 1K at the most.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP