I'm trying to write the return of terminal in a file, called debug.log. I would like also to get the pid to kill the process, for this moment the kill is working. But the debug.log is empty
cmd1 = "cvlc rtp://232.0.2.183:8200 --sout file/mkv:/media/file.mkv"
with open("/home/user/.cache/debug.log", 'w') as out:
proc = subprocess.Popen(cmd1, stdout=out, shell=True, preexec_fn=os.setsid)
pid = proc.pid
with open("/home/user/.cache/pid.log", 'w') as f:
f.write(str(pid))
f.close()
Edit: I'm using this method to kill the process and this method (from here) to write the log:
###########kill the process############
import os
import signal
import subprocess
# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before exec() to run the shell.
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
shell=True, preexec_fn=os.setsid)
os.killpg(pro.pid, signal.SIGTERM) # Send the signal to all the process groups
######### write the log #############
import subprocess
cmd = ['ls', '-l'] # example of command
with open('output.txt', 'w') as out:
return_code = subprocess.call(cmd, stdout=out)
In fact, I would like to mix both of examples. Thanks