Related questions:
This question ended up not requiring a resolution.
This question had a resolution not involving timeout
, but suggested that timeout
should work for this purpose when available.
My question:
This command produces no output into foo.txt
:
$ cat foo.sh
#!/bin/sh
sh -c "echo foo; sleep 5; echo bar" | awk '/foo/ {print $1}'
$ timeout 2 ./foo.sh > foo.txt
If I don't redirect into foo.txt
, I see foo
print out immediately as expected.
On the other hand, the following produces "foo" int the file foo.txt
as expected:
$ timeout 2 sh -c "echo foo; sleep 5; echo bar" > foo.txt
$ cat foo.txt
foo
Does anyone know why this may be happening and how best to resolve it? This is a toy example, but the actual script I'm running that led to this problem produces around 100 lines of output on the command line, but also leaves foo.txt
empty if it times out before terminating.