I want to be able to use Popen.communicate
and have the stdout logged to a file (in addition to being returned from communicate()
.
This does what I want - but is it really a good idea?
cat_task = subprocess.Popen(["cat"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
tee_task = subprocess.Popen(["tee", "-a", "/tmp/logcmd"], stdin=cat_task.stdout,
stdout = subprocess.PIPE, close_fds=True)
cat_task.stdout = tee_task.stdout #since cat's stdout is consumed by tee, read from tee.
cat_task.communicate("hello there")
('hello there', None)
Any issues with this, looking at communicate's impl it looks good. But is there a nicer way?