Sample Header Ad - 728x90

Unable to redirect stdout for background terraform process after it received input from named pipe

1 vote
0 answers
267 views
I have a terraform file:
terraform {
  required_version = "1.3.5"
}


locals {
  a = "foo"
  b = "bar"
}
in a bash terminal, I can do:
$ echo "local.a" | terraform console
"foo"
$ echo "local.b" | terraform console
"bar"
Now what I'm trying to do is start a process running terraform console in the background and feed it commands. This is what I've tried (following this answer https://serverfault.com/a/815253) :
$ mkfifo /tmp/srv-input
$ tail -f /tmp/srv-input | terraform console >>output.txt 2>&1 &
this starts the background process correctly:
$ ps -ax | grep terraform
6030 pts/0    Sl     0:01 terraform console
if I then run:
$ echo "local.a" > /tmp/srv-input
the output file, output.txt is empty.
$ cat output.txt
$
If I run:
$ echo "local.c" > /tmp/srv-input # invalid input
the output file, output.txt contains the (expected) error: $ cat output.txt ╷ │ Error: Reference to undeclared local value │ │ on line 1: │ (source code not available) │ │ A local value with the name "c" has not been declared. Did you mean "a"? ╵ + Exit 1 tail -f /tmp/srv-input | terraform console >> output.txt 2>&1 ---------- Why is only the stderr being redirected to the log file, but not stdout?
Asked by Foo (242 rep)
Dec 15, 2022, 02:07 PM