Bash – Redirect script output to /dev/tty1 and also capture output to file

bashio-redirectionlinuxteetty

I want to display text output on the console that is always displayed on a small screen on my Raspberry Pi.

The following code works for showing that text output:

cd /home/pi/python_test_scripts_linux && sudo nice -n -20 /home/pi/python_test_scripts_linux/test_wrapper.py > /dev/tty1

Now I want to capture the output in parallel with seeing it on the screen – I have tried 'tee' but that does not show text on the screen and also does not capture it to file:

cd /home/pi/python_test_scripts_linux && sudo nice -n -20 /home/pi/python_test_scripts_linux/test_wrapper.py | tee /dev/tty1 /tmp/capture.txt

How can I redirect the output of my script to /dev/tty1 so I can see it on my screen but also capture the output to file?

UPDATE 1:

Per the answer below – I tried using 'script' – unfortunately it did not work:

script -c "cd /home/pi/python_test_scripts_linux && sudo nice -n -20 /home/pi/python_test_scripts_linux/test_wrapper.py > /dev/tty1" /home/pi/python_test_scripts_linux/report.html

UPDATE 2:

I also tried to 'tail' the output of the file that I redirected the output to into /dev/tty1, but it also did not work:

sudo tail -F /home/pi/python_test_scripts_linux/report.html > /dev/tty1 &
cd /home/pi/python_test_scripts_linux && sudo nice -n -20 /home/pi/python_test_scripts_linux/test_wrapper.py > /home/pi/python_test_scripts_linux/report.html 

Best Answer

If you want to save the output of a command, use the script command

script -c "your command" /tmp/capture.txt

The output will be sent to the tty and also to capture.txt

If tty1 is not the console that you are running from, you could run a

tail -F /tmp/capture.txt 

from that tty in order to get the results there as well.

Related Question