Search code examples
linuxtee

tee doesn't write to screen or file


I use the following command to see which VirtualHosts are getting traffic on my server (alternatives welcome).

 ngrep 'Host:' port 80 -t  -W byline -q | grep  '^Host'

I want to be able to analyse the number of hits in a time interval while also seeing what's going on. To do this I have been trying to write the output to a file while also displaying it using tee.

 ngrep 'Host:' port 80 -t  -W byline -q | grep  '^Host' | tee ~/hosts.log

However, nothing is printed to screen or logged to file.

I have tried different file locations, outside of my screen session - with no difference. Does grep need special handling?


Solution

  • There is line buffering at play on the part of both ngrep and grep. Here is how you can get aroundthe issue you’re seeing:

    % sudo ngrep 'Host:' port 80 -t -W byline -d lo -q -l |grep --line-buffered '^Host:' |tee foo.log
    

    Notice that ngrep is using -l to make stdout line buffered, and grep uses --line-buffered for the same.

    (I added the -d lo just for my own testing.)