using "tail -f" in script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers using "tail -f" in script
# 1  
Old 05-20-2010
using "tail -f" in script

I am trying to use this script however it is not running well

Code:
#!/bin/sh
tail -f filename | grep -i -E 'error|warning'

the code does not display the first 10 lines of the file that I want to check
since after the file grow, it cannot grep "warning" as well
Code:
#!/bin/sh
A=`tail -f filename | grep -i -E 'error|warning'`

the code does not display anything.


can someone please comment?
# 2  
Old 05-20-2010
tail -f is a continous tailing of a file and not displaying the 1st 10 lines of a file. That would be a simple
Code:
head # defaults to 1st 10 lines

or
Code:
head -10

Also files usualy grow at the end of the file so you might want to be more precise if you want to check the latest 10 rows of a file or the 1st.

Rest of the grep is ok - but if it doesn't print anything it seems there is nothing in your file that matches.

The tail -f into a variable does not work since the defining of the variable does not end becuase the file you are tailing is kept open.
If you tell us what you want to do with the results inside a variable we can maybe show an alternative.
# 3  
Old 05-20-2010
I want to put `tail -f filename | grep -i -E 'error|warning'` into a variable and construct an e-mail that report any lines that contain "error" or "warning", after that, I will send the mail to the address specified in the RECIPIENT shell variable which is defined within the script.

I have tried `tail -f filename | grep -i -E 'error|warning'` in the terminal, there is no problem that it displayed last(sorry it is not the 'first' I just posted) 10 lines of the file which contain "error" and "warning" firstly, and then wait for the file to grow and check the next line if it is contain the keywords.

also , the `grep -i -E 'error|warning'`, is it correct that it can grep all the "error" or "warning" in any combination of upper and lower case letters?

Thanks!
# 4  
Old 05-21-2010
1. Yes the grep command which you had given and explanation are right.

2. you cannot use tail -f, use just tail.

Why did you want to use -f ?!
# 5  
Old 05-21-2010
I use tail -f because the file the script check will grow, the task is to handle the growing content in the file.
but since using tail -f is not wise because I have no idea how to put it in to a variable.
# 6  
Old 05-21-2010
A little shell script for the background:
Code:
$> cat logit.ksh
#!/usr/bin/ksh

LOGFILE=somefile

tail -f $LOGFILE| while read LINE; do
        echo $LINE| grep -q error && echo "`date` -- Error in [${LOGFILE}]!"| mail -s "Logfile warning!" you@bla.com
done

exit 0
$>
$> nohup ./logit.ksh &

If you want a bit more professional, check out swatch:
swatch: Automated Log Monitoring for the Vigilant but Lazy | Linux Journal
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

5. UNIX for Dummies Questions & Answers

"tail -n 1 filename" error while "head -n 1 filename" is ok?

Hi all, I was wondering why tail -n 2 filename produce an error when I manage to do similar command on head -n 2 filename SunOS{type8code0}: tail -n 2 filename usage: tail ] tail ] (2 Replies)
Discussion started by: type8code0
2 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

8. Shell Programming and Scripting

Perl - immitate perpetual "tail"

Is there some way I can have a script monitor the newest line of a log file until a certain entry gets placed in the log? Basically I want to start this script, and have it spit up the last 10 lines or so to a browser via AJAX... the AJAX is not my problem though, as I have no idea how to go... (4 Replies)
Discussion started by: jjinno
4 Replies

9. Shell Programming and Scripting

script not working after "tail -f"

Hi Everyone , I am facing a strange problem i have made the follwing script to watch a appending log file (abc.log) but its not moving after the line tail -f , any suggestions ===================================== #!/bin/bash while true do tail -f abc.log | grep "exceptions" echo hi... (12 Replies)
Discussion started by: xander
12 Replies
Login or Register to Ask a Question