0

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

Community
  • 1
  • 1
Guillaume
  • 2,752
  • 5
  • 27
  • 42

1 Answers1

1

You need to redirect the 'stderr' (not 'stdout') to 'out'.

proc = subprocess.Popen(cmd1, stderr=out, shell=True, preexec_fn=os.setsid)
arvindch
  • 2,385
  • 2
  • 13
  • 17
  • Thanks for you response, but it's the same. `with open("/home/user/.cache/debug.log", 'w') as subprocess.PIPE: proc = subprocess.Popen(cmd1, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)` The file is empty too – Guillaume Jun 28 '14 at 06:13
  • @Guillaume - I've added the code to access the output string as well. – arvindch Jun 28 '14 at 06:14
  • @Guillaume - The code I've provided works for the test cases on my system, however, I can't replicate yours. I'm not sure what sort of terminal output you're getting now, and what you need. Read the subprocess docs and see if that helps - https://docs.python.org/2/library/subprocess.html. – arvindch Jun 28 '14 at 06:36
  • I think the return of vlc is different, I have to add "preexec_fn=os.setsid" to kill the process. But I don't know really what is this – Guillaume Jun 28 '14 at 06:42