usustarr
usustarr

Reputation: 418

How to redirect Win cmd output "live" and not wait

1) I need to run an external program on Windows 7 in Python and redirect the "live" output(do not need STDERR) to a file call log.txt. Preferably append, not overwrite.
2) This external program will time to time log events into my output file, log.txt. 3) I do NOT want my python script to wait above #1 and #2 happening. In other words, I want my python scrip to do #1 and #2 and return so I can do other things.

I have tried following with not much luck, 1) ' |& tee ~' 2) '>>' 3)

with open(fileName,'w') as fout: subprocess.call(command,stdout=fout)

All of the above attempts caused my code to wait. I need to somehow issue a child process and not wait for the return. Can anyone help me out with this please?

Below code does log first subprocess call. But after that it doesnt log anything from the sendCmd(). If I were to manually run pdSniffer.py it will log to the same Event.log file.

def startListner():
  fileName =getScriptPath() +r'\Script\allscripts\eventLog.txt'
  path =getScriptPath() +r'\Script\allscripts\pdSniffer.py 198.168.1.101 '
  winCMD='C:\\Python27\\python ' +path
  fout =open(fileName,'w')
  subprocess.Popen(winCMD, stdout=fout)
  Delay(2000)
  sendCmd()
  Delay(360000)

def sendCmd():
  path =getScriptPath() +r'\Script\allscripts\sendCreateEvent.py 192.168.1.100 1041 1143 0 4 0'  
  p =subprocess.Popen(['C:\\Python27\\python', path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 

Upvotes: 0

Views: 317

Answers (1)

nanofarad
nanofarad

Reputation: 41281

You'll have to use the subprocess.Popen interface, which does not block unless you manually call wait:

proc = subprocess.Popen(command, stdout=fout)

Upvotes: 2

Related Questions