LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-24-2006, 01:54 AM   #1
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Rep: Reputation: 31
can output of tail be redirected?


Hi Everyone, Can the output of tail be redirected while it is running.... For example......if I do


$tail

.....Then can I compare a particular string in the output of tail to another string written in a shell script

Thanks
Danish
 
Old 04-24-2006, 02:11 AM   #2
LMAC
LQ Newbie
 
Registered: Apr 2006
Location: Sydney
Distribution: Ubuntu
Posts: 3

Rep: Reputation: 0
Well to do that you probally have to store the second string as a variable and do a compare with the tail output... well just compare two variables.
 
Old 04-24-2006, 09:29 AM   #3
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
yes, you can
 
Old 04-24-2006, 11:38 AM   #4
kriton12
Member
 
Registered: Oct 2002
Posts: 81

Rep: Reputation: 16
Yes. If you are using bash try an example like this:

#!/bin/bash

stuff=$(tail -1 /var/log/messages)
echo $stuff

(note you will probably need to run this as root since /var/log/messages is most likely not readable by everybody. Just substitute your text file and path for /var/log/messages).

This will save the output of the tail command into the variable stuff. From there you can use whatever technique you want to parse your string.
 
Old 04-24-2006, 11:50 PM   #5
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Original Poster
Rep: Reputation: 31
Yes we can save the output to a variable, but then that is if I do
#tail -1
But I want to do
#tail -f
and then mail the administrator of the system whenever a particular string appears

Thanks
Danish
 
Old 04-25-2006, 03:30 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
You can redirect to a file
Code:
tail -f logfile > otherfile
Or you can pipe it to grep
Code:
tail -f logfile | grep mysearchstring
In the second example, you can check the result of grep and take decisions based on that.
'tail -f' will not work in the second example as tail will never end, so the code has to change to something like:
Code:
#!/bin/sh
if tail -100 /var/log/apache/error_log |grep -i willempie; then
  echo OK
fi

Last edited by Wim Sturkenboom; 04-25-2006 at 03:32 AM.
 
Old 04-25-2006, 05:15 AM   #7
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Using Wim Sturkenboom's script and running it regularly (ie each 5 minutes for fast changing log) via crontab
may indeed be a solution.

If you don't want that, you can also write your own shell script that does
an infinite number of reads, until stdin is closed (read fails).

The algorithm would be something like:
while(1)
{
$a=read();
if (read_failed)
{
break loop;
)
if ( a matches string )
{
send mail
}
}
 
Old 04-25-2006, 05:57 AM   #8
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Original Poster
Rep: Reputation: 31
Yes very true,
tail -f logfile does'nt run in a bash script

If i store the output of $tail -f logfile in a file, say X, and then compare a string written in a bash script to a string in X,
I think the output would somewhat be what I want....

But I wanted to know can't we do

$tail -f logfile | mail ab@xy.com

Thanks
Danish
 
Old 04-25-2006, 09:16 AM   #9
kriton12
Member
 
Registered: Oct 2002
Posts: 81

Rep: Reputation: 16
Yes, I'm pretty sure you could do that direct

tail -f logfile | mail -s "subjectline" email@address

The only problem is that I'm not sure if the -f option in tail ever lets it end, in which case you need to decide when you want to mail the output because the mail command needs an EOF to signal it to send the mail. So as Sturkenboom says, either give it a --lines=### or --byte=### option (-n and -c respectively) so that it knows when to send instead of -f.

This is what I think I would do...

tail -$(wc -l /path/to/logfile | gawk '{print$1}') | mail -s "subjec" email@address

First it finds the number of lines in the logfile by using 'wc -l' which is then piped to gawk to parse the output.. of which you only want the first piece. This basically gives you the number of lines in the logfile which is substituted as the number of lines argument in the tail command, so just the tail -$(wc -l /path/to/logfile | gawk '{print$1}') piece prints out all the lines of the log file using the tail command. That output is then mailed to the email address.

But now that I think about it, isn't this just a fancy way of saying:

cat logfile | mail email@addressDOTcom

?

What exactly do you want to do, have the output emailed to you each time it is updated? You may want to use cron or even tripwire to do that instead. Otherwise I guess you could write a bash script that wakes up every minute or so, checks the file with the diff command and if something changes, email the changes or the entire contents of the file.

Last edited by kriton12; 04-25-2006 at 11:32 AM.
 
Old 04-25-2006, 11:58 PM   #10
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Original Poster
Rep: Reputation: 31
I do understand that mail@someonesaddress.com would need an EOF to execute and that tail -f does not provide that...Although I have thought of an idea, but it requires two scripts to be made and ideally I dont want that....

What I had in my mind was...
$tail -f /logfile >> logfile1.txt

1) then as the data in logfile1.txt would change every now and then

2)I would write a bash script which has a variable containing a paricular string and then this particular string is then matched with the current last line in logfile1.txt.....

3)As soon as the bash script finds a match in logfile1.txt a mail would be sent to xyw.com containing that particular string

Thanks
Danish
 
Old 04-26-2006, 01:39 AM   #11
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Have you looked at
man logrotate ?

Logfile rotation is pretty standard stuff and can include mailing the log to someone.
Maybe there's an option (ie via scripting) to mail only if the log has a certain string.
 
Old 04-26-2006, 03:16 AM   #12
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Using 'tail -f /logfile >> log1.txt' seems pretty useless to me.

But I think that we have all ingredients to create a solution. I really enjoyed building the following. As I'm not a bash scripter, I learned a lot.
Code:
#!/bin/sh

# file and searchstring
myfile="testfile.txt"
mysearchstring="mutex"
# loop delay
mydelay=5

# initial number of lines in file
result1=$(wc -l $myfile | awk '{print $1}')

# loop forever; I don't know another way
i=1
while [ $i -eq 1 ] ; do
    # get new number of lines and calculate difference
    result2=$(wc -l $myfile | awk '{print $1}')
    difference=$(($result2-$result1))
    # if different
    if [ $difference -gt 0 ] ; then
        # send mail
        tail -$difference $myfile |grep $mysearchstring |mail -s test root
        # reset initial result to current number of lines
        result1=$result2;
    fi

    sleep $mydelay
done
Some flaws:
1)
each entry in the log must start on a new line
2)
concurrency; if the logfile is updated by the application after wc is executed by the script but before the script executes the tail, you might miss some lines
3)
if logfile is deleted or manually made smaller, script has to be restarted !!

Flaw 3 can be fixed in the code, I guess you have sufficient info now. Also, you can add code to send the initial log when the script is started (or restarted).

Last edited by Wim Sturkenboom; 04-26-2006 at 03:29 AM. Reason: added flaw 3
 
Old 05-02-2006, 11:44 PM   #13
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Original Poster
Rep: Reputation: 31
Thanks I do appriciate the help that you all are giving but its kind of becoming messier..Im still a newbie and Ive just joined office on probation as a trainee So its tough trying to cope up with different kinds of information....I think the script that I want will take a lot of deep understanding by me..Sadly I have to leave it here. But thank you all for helping me...
And Wim Sturkenboom..I was not able to get the correct ouput by runnning your script...I tried understanding it but I think it required more attention from me but sadly Im not able to give that....

Thank you all
Danish
 
Old 05-03-2006, 01:42 AM   #14
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
If you can come back with more details sometime, we'll be able to help you better.
 
Old 05-03-2006, 01:57 AM   #15
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Original Poster
Rep: Reputation: 31
Thanks..Ill surely come back when Im better..Meanwhile Ill continue posting the small problems that I have

Danish
 
  


Reply

Tags
bash, script, source


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
iptables - why am I still being redirected? michaelsanford Linux - Networking 1 05-23-2005 03:52 PM
GPG and redirected input Vookimedlo Linux - Software 1 04-30-2005 04:32 PM
Is a Domain being redirected? rigel_kent Programming 0 02-14-2005 06:43 AM
my domain is getting redirected to a different ip. QS_Shorty Linux - Newbie 3 02-10-2004 08:22 PM
Some windows redirected to console (not in VNC) jswilson Red Hat 6 10-31-2003 03:22 PM


All times are GMT -5. The time now is 01:29 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration