I have a process that outputs logs to a file, and I'm trying to make a python script to run that process and output only the log that it generates into that log file.
What I wrote below seems to do what I want except for one big issue: it never exists the while loop. I've tried a few other alternatives with no luck.
Any help here would be really appreciated! I guess it's worth noting I could also use node script instead...
import subprocess
import sh
process = subprocess.Popen(cmd)
log_tail = sh.tail("-f", log_file, _iter=True)
while process.returncode is None:
sys.stdout.write(log_tail.next())
sys.stdout.flush()
process.poll()
The tail -f
command is intended to run until explicitly interrupted, which is why the loop is never exited. You could try this:
import subprocess
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
which will wait until the cmd
terminates and return the contents of its output streams in stdout
and stderr
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.