Along the lines of what you already have (tested with python3
):
tee -a output.txt | python ask.py 2>&1 | tee -a output.txt
As a downside, you will need to explicitly type something after the python script terminates, to make the first tee
attempt writing to the pipe, receive a SIGPIPE
and exit. You may be able to overcome this limitation with:
tee -a output.txt | { python ask.py 2>&1; kill 0; } | tee -a output.txt
Where kill
is used to kill all the processes in the current process group (i.e. the dedicated process group the pipeline is run as). (But note that caveats may apply).
An alternative, if script
is available to you, may be:
script -q -c 'python ask.py' output.txt
In this case python
will be connected to a pseudo-terminal device, ensuring it behaves as if it were run interactively on a terminal.
logging
module,input()
andprint()
. I'm trying to modify that currently, if there just isn't a simple Linux solution. But a simple Linux solution is much preferred over modifying my Python script. What if say the Python script is a system script that I can't modify.