So I asked a question before that added a prefix to a ping. (My last questions) that left me with the following line:
ping 8.8.8.8 | while read line; do echo "$(date): $line"; done | grep time=
And this works GREAT. I have only one problem I can not get this to save it in a file. I tried it with just a simple redirect like so:
ping 8.8.8.8 | while read line; do echo "$(date): $line"; done | grep time= >> googleping
But nothing gets saved in the file...
Then I tried this:
ping 8.8.8.8 | while read line; do echo "$(date): $line"; done | grep time= | tee -a googleping
with tee to print it on screen and also save it in the file... no luck again.
(But tried echo hello | tee -a googleping
and it worked fine...)
So and then I tried another while loop like so:
ping 8.8.8.8 | while read line; do echo "$(date): $line"; done | grep time= | while read line; do echo $line; echo $line >> googleping; done
No luck again...
So is there a limit on how many pipes and redirects one line can have? And if so is there a way that I can still achive my goal of logging when I can't reach google (I just tested it with grep time=
to have garuenteed output and will use grep -v time=
to get all lines that have no time in them no matter waht the error may be)
So to add is that in the end I want to do in in the mac terminal, but I tried it on ubuntu server and a mac, and neither work with any of the methodes described above.
I hope someone can help me!
tee -a
means append. So if somehow you've created a file with lots of blank lines at top, your data is at the bottom. For testing, I'd recommendtee file
or...> file
. If osx hasstdbuf
(I don't think it does), try adding that to the very front of your pipe list, and dbl-check how to use it withman stdbuf
. Finally, Max pipes will be a minimum of 10-20. That shouldn't be a problem . Pluse-uno for really drilling down on your problem. Good luck.googleping
to see if the file has any size, r.e. my comment on append. Good luck.grep --line-buffered
instead to flush the buffer after every line. Alternatively, use the-c
option ofping
to stop after sending a certain number of packets; when it stops you'll immediately get your stuff.