Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Showing results for 
Search instead for 
Did you mean: 

another scripting question

Go to solution
Bryan D. Quinn
Respected Contributor

another scripting question

I have a bit of a scripting dilemma that I need some help with. I need to come up with, if possible, a way to monitor a log file for a certain line of text. This exact line is written to the script multiple times during our nightly backup, but for each instance I need to capture it as it is written to the file. When this happens I will be triggering another event. I have racking my brain trying to come up with a solution. Basically I need something that would act like tail -f, but from a script.
Anybody with any ideas?
I don't need specific code, just some help with a way to approach the problem.

Bryan D. Quinn
Respected Contributor

Re: another scripting question

Oops! Sorry, that should not have been databases for the category.

A. Clay Stephenson
Acclaimed Contributor

Re: another scripting question

Actually you were rather close:



tail -f ${INFILE} | while read X
echo "${X}"

Create a file "myfile" and start the above script. Now do something like echo "XXX" >> myfile and you will magically see the new line of onput appear. Your processing goes where the current echo "${X}" is.

If it ain't broke, I can fix that.
Kent Ostby
Honored Contributor

Re: another scripting question

Well depending on your time tolerance, you could do something like copy the log file to a temp log file.

Compare that to the copy you made 5 minutes ago and then grep for the line you are looking for and react if you find it.

You can put this all inside a loop with a sleep.

Loop around Something like:

sleep 300
cp logfile ms.newlogfile
diff ms.oldlogfile ms.newlogfile > ms.useme
grep "yourstring" ms.useme > ms.useme2
if [ -s ms.useme2 ]
do your other stuff

Then close the loop

The sleep 300 is your 5 minute delay.
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Bryan D. Quinn
Respected Contributor

Re: another scripting question

Thanks Clay!

So simple, yet so far away! Thanks for clearing up the water for me.