74 lines
1.6 KiB
Python
74 lines
1.6 KiB
Python
#!/usr/bin/env python
|
|
|
|
import sys, os
|
|
import subprocess
|
|
|
|
def runCmd( cmd, fifo ):
|
|
try:
|
|
os.unlink(fifo)
|
|
except:
|
|
pass
|
|
|
|
os.mkfifo( "out_fifo" )
|
|
|
|
try:
|
|
|
|
fifo = os.fdopen( os.open( "out_fifo",
|
|
os.O_RDONLY | os.O_NONBLOCK ) )
|
|
|
|
newcmd = "( %s ) 1>out_fifo 2>&1"%( cmd, )
|
|
|
|
process = subprocess.Popen( newcmd, shell = True,
|
|
stdout = subprocess.PIPE,
|
|
stderr = subprocess.STDOUT )
|
|
|
|
_localLog.debug( "Running: %s"%( cmd, ) )
|
|
|
|
while process.returncode == None:
|
|
# None means process is still running
|
|
|
|
# need to poll the process once so the returncode
|
|
# gets set (see docs)
|
|
process.poll()
|
|
|
|
try:
|
|
line = fifo.readline().strip()
|
|
except:
|
|
continue
|
|
|
|
if line:
|
|
log.info( line )
|
|
|
|
remaining = fifo.read()
|
|
|
|
if remaining:
|
|
for line in [ line
|
|
for line in remaining.split( "\n" )
|
|
if line.strip() ]:
|
|
log.info( line.strip() )
|
|
|
|
if process.returncode:
|
|
_localLog.critical( "Return Value: %s"%( process.returncode, ) )
|
|
else:
|
|
_localLog.debug( "Return Value: %s"%( process.returncode, ) )
|
|
|
|
finally:
|
|
|
|
os.unlink( "out_fifo" )
|
|
|
|
|
|
mainlog.debug( os.getcwd() )
|
|
|
|
print
|
|
runCmd( "echo 'bye'", mainlog )
|
|
|
|
print
|
|
runCmd( "/usr/bin/false", mainlog )
|
|
|
|
print
|
|
runCmd( "ls -l; sleep 5; echo 'hi!'; sleep 5; ls -l", mainlog )
|
|
|
|
print
|
|
runCmd( "this_should_not_exists", mainlog )
|
|
|