I was writing a Terraform module for AWS EC2 that involved executing a bash in the user data section. While I was developing I had an issue in the script I wrote but neither AWS nor Terraform provided any logs for the errors in I got until I found this line in AWS support forum:

exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1

This line writes all the output of the script executed in user_data to /var/log/user-data.log successfully but I don't understand the whole line. I know exec > writes all the output to a file, which in this case >(..) but I don't understand why it's using tee or the need for a pipeline there.


Let's break that in to pieces:

exec applies redirections on the shell itself, so indeed exec > somefile would direct all later output from the shell and its children to that file.

Here, instead of a simple file name, we have the process substitution >( command... ). It tells the shell to run the command inside, and to create a some filename that's connected to the input of that command. foo > >(bar) is a bit like foo | bar, except that process substitutions can be used for more complex cases too, and don't invoke subshells like the pipeline does. (<(command...) would be the reverse, it would make the output of the command appear as if from a file.)

The command inside is tee filename | logger .... tee copies its input both to stdout, and to the given file(s), and logger sends its input to the system log. (The redirection 2>/dev/console would apply to error output from logger, probably it would print some errors if the system log doesn't work.)

Then there's the 2>&1, which tells the shell to redirect error messages (stderr) also to that same process substitution.

So, after the redirection, everything the shell outputs (normal output and error output) goes (via tee) both to the file and (via logger) to syslog.

(I don't think any of that is specific to AWS or Linux, unless of course there's some particularities about the use of logger on AWS.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.