I am writing an installer script and need to run it a sudo
as I test it on AWS RedHat instances, so can't directly go as root
. I want to write the log into a file as well as onto the screen. Nothing should be easier than that,
I can either go old school >> /tmp/Solr_Install.log 2>&1
and start a tail process in the background: tail -f installer.log &
This solution tends to duplicate or triple the output on the screen, so I opted for a tee-totaller solution.
Just use the power of the force and 2>&1 | tee -a /tmp/Solr_Install.log
With this solution, I lose my sudo rights, and the script fails.
Questions:
Why does the tail show the same line 2-3 times? (Has it got something to do with the fact that tail -f
displays the last 10 lines?)
Can this be fixed somehow?
Why do I lose my sudo when I tee?
Can this be avoided?
Is there any other way to throw log materials into a file and onto the screen at the same time?
exec > >(tee "/tmp/Solr_Install.log") 2>&1
is the best for you.