Tail -f alternative ?

I am trying to do this in shell

tail -f console.log | grep "some string" >> console.sel &

However, this does not produce any output.

The purpose is to watch for messages from Growl to intercept iTunes change track message to record tracks played.

Is there an alternative_

iMac 20, Titanium, iMac 24" C2D, Mac OS X (10.4.8), some wintel :-(

Posted on Sep 16, 2007 4:09 AM

Reply
9 replies

Sep 16, 2007 8:01 AM in response to Lanny Rosicky

I think your problem is that stdout is buffered. The output of tail is buffered, and grep isn't going to see any input until the buffer is flushed. That won't occur until either the buffer is full, or the program feeding the pipe exits. But tail -f doesn't exit. The log jam continues. If and when grep finally does see some input, its output is also buffered. Nothing's going to show up in your output file until the buffer is full, or the program feeding grep exits so that grep can exit.

I don't think you're going to have much luck doing what you want with a shell script. Not enough control over io buffering. I don't think it'd be that tough to do it with perl, though.

Sep 16, 2007 10:00 PM in response to Lanny Rosicky

Have you done any diagnostics?

For example, what happens if you do

tail -f console.log

without the pipe to grep? What happens if you do

grep "some string" console.log

without the tail? If either of those commands does not produce output, then figure out how to get it to give output before worrying about doing them in combination.

I have tested, for example,

tail -f /var/log/ipfw.log | grep Stealth

and I do get output every so often.

Do you get output if you don't put it in the background? Is their an interaction between the redirection and the backgrounding?

Sep 17, 2007 2:53 AM in response to Don MacQueen1

Interesting, Don. That works precisely as you describe, but if you try to send the output to a file, the output gets lost somewhere. I still think that's buffer land, though pipes don't seem to still be buffered as I remember.

mac:~ $ tty
/dev/ttyp1
mac:~ wmp$ tail -f /var/log/secure.log | grep sudo
Sep 15 08:50:41 mac com.apple.SecurityServer: ...
... four more lines of the same ...
... and subsequent lines are displayed immediately. But ...
^C
mac:~ $ tail -f /var/log/secure.log | grep sudo >sulog

... leave that running, switch windows ...
mac:~ $ tty
/dev/ttyp2
mac:~ $ cat sulog
mac:~ $ sudo ls > /dev/null
Password:
mac:~ wmp$ cat sulog
mac:~ wmp$

... switch windows, kill tail -f, switch back ...
mac:~ $ cat sulog
mac:~ $

So where's the output? I'm sticking with lost in buffer land.

Sep 18, 2007 5:36 AM in response to biovizier

It's because stdout is buffered. Your confusion didn't seem to affect your ability to come up with an elegant solution, though.

mac:~ $ log=/var/log/secure.log
mac:~ $ tail -f $log | while read i; do echo $i | grep /bin/su >> log; done &
[3] 2127
mac:~ $ wc log
10 171 1594 log
mac:~ $ su
Password:
su: Sorry
mac:~ $ wc log
11 189 1748 log
mac:~ $

This may very well be portable these days, but I don't think it's backward compatible to ancient versions of Unix. Depends on precisely how the shell implements pipes. If they're buffered in the same way output redirection is buffered, this won't work.

Message was edited by: B3and88

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Tail -f alternative ?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.