I am having difficulty redirecting the output of a process created using subprocess.Popen to a file of my choice. The following is what I am doing:
#popen_test.py import sys, os, subprocess, time, signal process_log = open('process.log','w') process = subprocess.Popen([<path_to_python_binary>,"process.py"], stdout=process_log, stderr=process_log, preexec_fn=os.setsid) #launching the sub process time.sleep(10) #waiting for sometime; this allows the sub process to print some statements. See below process_log.flush() os.killpg(process.pid,signal.SIGTERM) process_log.close() And, this is how my process.py looks. It is basically printing some statements on stdout at regular intervals:
#process.py import sys,os,time print "Initially..." while True: print "Hello World" time.sleep(1) The process is being launched as I can see using ps, but the output is not going to the process.log--the file remains empty. Any idea what I might be doing wrong?
process_log = open('process.log','w')p.terminate()withoutsetsidhack here becauseprocess.pydoes not create its own child processes.