0

I have file named test.py and I am trying to run below command as a background process as part of this script

"tail -n0 -f debug.log" 

Also, I want this process to end as soon as test.py execution is completed. However, I can't get this to working. I have tried below code but tail command not exiting even after main script is completed. I am new Python, can someone help me do this clean way ?

pro = subprocess.Popen(["tail", "-n0", "-f", log_file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in pro.stdout:
    print(line)
os.killpg(os.getpgid(pro.pid), signal.SIGTERM)
Tasmiya
  • 1
  • 1
  • The OS will implement buffering between your Python process and the subprocess. You can get around that with `bufsize=1`. I'll try to find an appropriate duplicate. – tripleee Feb 17 '22 at 10:05

1 Answers1

0

I used it once before, so I can't remember exactly, but I think I exiting 'subprocess.Popen' using 'with'. I'm not sure, but I recommend giving it a try.

with subprocess.Popen(["tail", "-n0", "-f", log_file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as pro:
    for line in pro.stdout:
        print(line)
Desty
  • 328
  • 1
  • 5