I have a large bash script (Ubuntu 12.04). The script is designed to be invoked by a regular user; only certain commands within the script run with sudo. It is not an option to run the entire script as root.
Within the script I have a lot of statements similar to these:
echo "blah blah" | sudo tee /path/to/file
sudo tee /path/to/file > /dev/null << 'END' blah blah END
The form of those statements is largely due to the script being designed to not run as root. I put a lot of work into getting those statements right. (It was a learning experience for me.)
The script works well and I do not want to massively change it. In fact, I don't want to change it at all because it is debugged and working in a production (sort of) environment.
However, I do want to add better logging. So I added the following lines to the beginning of my bash script as per https://stackoverflow.com/a/11886837/463994
#!/bin/bash >foo.log exec > >(tee -a foo.log) exec 2> >(tee -a foo.log >&2)
However, now when I run the script it asks me for my password for every instance of the sudo/tee command examples shown above (about 50 of them!).
Is there a solution for this that does not require me to significantly change the existing code? I just want to log the output of the script without changing how it runs (including without requiring password entry many times within a short span of time).
Here is code showing the problem:
#!/bin/bash sudo -k >foo.log #truncate log file to start fresh exec > >(tee -a foo.log) exec 2> >(tee -a foo.log >&2) if [ -f fileone.txt ] ; then echo "sudo rm fileone.txt" sudo rm fileone.txt fi echo "sudo touch fileone.txt" sudo touch fileone.txt echo "sudo touch filetwo.txt" sudo touch filetwo.txt sudo touch /opt/file3.txt echo "blah blah" | sudo tee /opt/file3.txt sudo rm /opt/file3.txt exit
enter password once and whole script runs
have to enter password again for
echo "blah blah" | sudo tee /opt/file3.txt and any similar statements of the form given at the top of this question. But only if the logging code at the top of the script is used. Without the logging, there is no problem.