Visit Our UNIX and Linux User Community


using tail -f


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using tail -f
# 1  
Old 10-23-2002
Question using tail -f

Working in HP-UX 10.20. I eventually want to write a bourne shell script to handle the following problem, but for now I am just toying with it at the command line.

Here's what I am basically trying to do:

tail -f log_X | grep n > log_Y

I am doing a tail -f on log_X . Once it sees "n", I would like for it to grep it, then put it into log_Y. It ain't making it to log_Y.

I have been manually adding "n" to log_X, and the tail command is definitely seeing it, but it fails to pass it to log_Y.

Why? Is it because the command is trying to "complete" the tail -f before it executes the > (redirect) to log_Y??

Is there a better way to appraoch this?

TYIA
# 2  
Old 10-23-2002
try using

tail -f log_X | grep n >> log_Y

I'm not sure this would work...

I can add that the commands on either side of a pipe "|" are started and executed synchronously... and we know that any command like grep will finish only if it sees an EOF, which "tail -f" will not give as it is in an infinite loop looking for newly appended lines...

so...

tail -f log_X | grep n

should be working fine and giving "new" lines having "n" to stdout as and when they append to log_x...

It may be a problem with redirection, so use redirection in append mode >> and let us know!!

Cheers!
Vishnu.
# 3  
Old 10-23-2002
Vishnu:

I tried using tail -f log_X | grep n >> log_Y yesterday...it DID NOT work. I also tried a tail -f log_X | grep n >! log_Y. No go.

The reason I am using tail -f is because I want this to be a "real-time monitor". Once "n" appears, I need it to notify me.

Any other ideas?

TYIA
# 4  
Old 10-23-2002
see these posts.. it seems that piping "tail -f" works with some OSes and does not work with some...

http://www.computing.net/solaris/www...orum/1734.html

http://www.zsh.org/mla/users/1999/msg00360.html

you did not tell whether...

tail -f log_X | grep n

worked on your system.. i.e., you can see the stuff on your terminal..

Cheers!
Vishnu.
# 5  
Old 10-23-2002
daemon process gives real time

It seems that you would be better served to create a daemon to control this process.

You could tailor it to notify you when the string you want appears and email or beep you. You should have a template of one on your system or you can copy from a simple one in a directory similar to /sbin/init.d/template.

cp template myscript

You will need to add a kill and start link in your rc directories for it to startup a boot time.

ln -s myscript /sbin/rc3.d/S400myscript
ln -s myscript /sbin/rc1.d/K400myscript


Hope this helps! Smilie
# 6  
Old 10-23-2002
Try this:
Code:
(tail -f /var/log/messages & ) | grep -i login

If you let tail run in the backgroundit lets grep work. As tail dumps out to stdout, grep is patiently waiting, reading stdin.

This will just dump out to your screen until you hit something like ctrl+c. If you want something more elaborate, you could sent the tail output to a different fd, and have grep read in from that fd.

Good luck!
# 7  
Old 10-23-2002
Oh heck, why not - here's a slightly improved design. Run it in the background, and it will write the user defined above when it sees the exact phrase (also defined above).

It's not the pertiest, and probably not the fastest if the logs grows very quickly, but it works, and it's be easy to modify to mail, page, whatever...
Just be careful that it doesn't flood you out if it finds the same message hundreds of times...

Code:
#! /bin/ksh

search_word="search terms"
write_user=user_id

tail -n1 -f /path/to/log |&
while read -p output_line; do
 [[ $output_line == *"$search_word"* ]] && {
  print "$output_line" | write $write_user
  }
done


Previous Thread | Next Thread
Test Your Knowledge in Computers #668
Difficulty: Medium
IEEE 802.11 is part of the IEEE 802 set of LAN protocols, and specifies the set of media access control (MAC) and physical layer (PHY) protocols for implementing virtual local area network (VLAN) Wi-Fi computer communication in various frequencies.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tail +

because the tail +2 on the first line gives me the file name pomga I do not want anything like what I miss tail +2 ejemplo.txt ouput ==> ejemplo.txt <== 1 2 3 4 5 6 7 8 9 10 (2 Replies)
Discussion started by: tricampeon81
2 Replies

2. Shell Programming and Scripting

Joining multiple files tail on tail

I have 250 files that have 16 columns each - all numbered as follows stat.1000, stat.1001, stat.1002, stat.1003....stat.1250. I would like to join all 250 of them together tail by tail as follows. For example stat.1000 a b c d e f stat.1001 g h i j k l So that my output... (2 Replies)
Discussion started by: kayak
2 Replies

3. Shell Programming and Scripting

tail for 15 mins

Hi, I want to write a script which will tail a particular file for 15 mins and then sleep for 10 mins and again tail for 15 mins. This cycle will go on for a limited period of time. How can i ensure that tail command will run for 15 mins before calling sleep command Thanks (6 Replies)
Discussion started by: @bhi
6 Replies

4. Shell Programming and Scripting

using tail -f and tr in a script

I have file that is being constantly written to example: file.txt ABC EBC ZZZ ABC I am trying to create a simple script that will tail this file and at the same time using tr to change B to F on lines containing 'B'. I tried this and it doesn't seem to work. #!/bin/bash tail -f... (8 Replies)
Discussion started by: zerofire123
8 Replies

5. UNIX for Dummies Questions & Answers

Need help with a tail and a grep

I need to tail -f a file so I can monitor it as it is being written to. However, there is a lot of garbage in the file that I don't care about. So normally I would just pipe and grep for the string that is important to me. However, in this case, there are two things I need to grep for. I can't... (3 Replies)
Discussion started by: Silver11
3 Replies

6. Shell Programming and Scripting

Help with tail command

Hi All, My query seems to be silly but Iam unable to find where the exact problem lies. I have a script to unzip set of files here is the script #!/bin/ksh Count=`cat /home/gaddamja/Tempfile | wc -l` while do Filename=`cat /home/gaddamja/Tempfile |tail -$Count | head -1` cd... (7 Replies)
Discussion started by: jagadish_gaddam
7 Replies

7. Shell Programming and Scripting

tail | grep

The program that is running on my machine generates log files. I want to be able to know the number of lines that contain "FT" in the most recent log file. I wrote the following, but it always returns zero. And I know the count is not zero. Any ideas? ls -rt *.log | tail -n 1 | grep -c FT (6 Replies)
Discussion started by: sdilucca
6 Replies

8. Shell Programming and Scripting

Tail Question

Hi All, I notice that the below tail cannot be done. How can i modify this code such that i can always "tail" a variable number of lines ? set num = 100 cat filename|grep xxx| tail -$num (5 Replies)
Discussion started by: Raynon
5 Replies

9. Shell Programming and Scripting

tail -f

I am trying to extract a particular line from a.log which keeps appending every sec and output that into a newfile b.log which should append itself with filtered data received from a.log I tried tail -f a.log |grep fail| tee -a b.log nothing in b.log tail -f a.log |grep fail >>b.log ... (4 Replies)
Discussion started by: wannalearn
4 Replies

10. Shell Programming and Scripting

Tail??

Hello all, I have search the forum and could not find an answer...Here is what I am trying to do. Every 15 minutes, a script send uptime output to a logfile (dailylog.log), that file contains lines like the one below: 11:21am up 44 days, 19:15, 1 user, load average: 0.00, 0.02, 0.03 ... (7 Replies)
Discussion started by: qfwfq
7 Replies

Featured Tech Videos