I want to use python to minic bash command ./script.sh &> /tmp/my.log &
, but with Python code able to modify the lines before they’re written to the output file.
import subprocess
import threading
import sys
log_file = sys.argv[1]
program_and_args = sys.argv[2:]
with open(log_file, 'w') as log:
process = subprocess.Popen(program_and_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, start_new_session=True)
print(f"Background process started. PID: {process.pid}")
def capture_output():
lineno = 1 # example, real logic more complicated
for line in process.stdout:
log.write(f'{lineno}: {line}')
log.flush()
lineno += 1
process.wait()
# capture_output()
thread = threading.Thread(target=capture_output, daemon = True)
thread.start()
print("Main program continuing...")
Although the above python program can run as below.
./main.py /tmp/my.log ./script.sh
But nothing is written in /tmp/my.log
. How can I fix the python program so that it has the same effect as the bash command ./script.sh &> /tmp/my.log &
with the addition of being able to modify the content with arbitrary Python code?
6