0

I need to run some bash script with python and show the output. Some of these scripts contains some pv command but from subprocess I am not able to get the pv output.

import subprocess as sp

p = sp.Popen(["./script.sh"], shell=False, bufsize=1, stdout=sp.PIPE, stderr=sp.STDOUT, universal_newlines=True)

p.wait()

print(p.returncode)
print(p.stdout.read())
#!/bin/bash

set -e

echo "aaa"
echo "bbb" >&2

pv -L 1000 -F "%t %b %r %e" /path/to/some/file | cat > /tmp/foo

Runnig this python script I simply get echo's outputs:

$ python script.py 
0
aaa
bbb

Red
  • 664
  • 2
  • 10
  • 20

1 Answers1

1

pv is sensitive to whether it is running in a terminal. If you want to capture its output, use the pv -f option to force it to display the progress indicator even when it's not connected to a terminal.

For more complex scenarios, you might have to use something like pty to simulate running under a terminal.

(Of course, you might be better off using a native Python progress bar like tqdm.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks, using `pty` I can make it work but my use case is pretty complex (multiple scripts to run in parallel) and using `pty` became difficult. Anyway I found that `pv` has a `-f` flag that: "_Force output. Normally, pv will not output any visual display if standard error is not a terminal. This option forces it to do so._" and this solves my problem – Red Dec 15 '21 at 14:20
  • Oh good, I'll add that to the answer. I meant to check but got distracted. – tripleee Dec 15 '21 at 14:21