【发布时间】:2014-08-19 06:58:21
【问题描述】:
我正在尝试将终端返回的结果写入一个名为 debug.log 的文件中。 我还想让 pid 终止进程,因为此时 kill 正在工作。 但是 debug.log 是空的
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()
编辑:我正在使用这个method 来终止进程 还有这个方法(来自here)写日志:
###########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)
事实上,我想混合使用这两个示例。 谢谢
【问题讨论】:
-
另外,f.close() 是多余的,因为 'with' 关键字会自动为您关闭文件。
-
啊好吧,我不知道!
-
是的,我试过了,但文件总是空的......我正在尝试用你的回复做不同的事情
-
试图适应这个response,但对我来说并不容易!
-
我已经编辑了我的问题,希望对你有所帮助
标签: python logging subprocess vlc