1

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?

3
  • Maybe this could be the case that fits your needs unix.stackexchange.com/questions/61931/…
    – Echoes_86
    Commented Jan 3, 2017 at 14:27
  • Echoes_86, I need to beg for your forgiveness. I doublechecked and your solution, well technically, @BatchyX solution works. I still don't know why I lose my sudo mojo, but hey, at least I have a working solution. Commented Jan 3, 2017 at 20:41
  • I'm happy you found your solution. For me, the approach (as I understand from your question) of exec > >(tee "/tmp/Solr_Install.log") 2>&1 is the best for you.
    – Echoes_86
    Commented Jan 3, 2017 at 22:11

1 Answer 1

1

sudo is not a privilege specific to the session. sudo is a command that runs other commands with elevated privileges. So just run your tee with sudo:

2>&1 | sudo tee -a /tmp/Solr_Install.log W
1
  • It is not the tee that does not work, it is the rest of the script that fail because of tee. Please do not upvote this answer as is, no offence, because it is not an satisfactory answer. Commented Jan 3, 2017 at 20:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .