linux - Executing processes with python by subprocess library -
i've created software in python should schedule several commands shell. (ps: i'm working on linux) way, i've created following function launch processes:
def exec_command(cmd): process = subprocess.popen(cmd, stdout=subprocess.pipe, shell=true, preexec_fn=os.setsid) pid = process.pid print '----------launched process pid: ', pid return pid
after use pid check if process goes on zombie's state , in case kill it. sometime works fine there case in process launched doesn't anything. process i'm talking is: sh /home/ubuntu/programs/data-integration/kitchen.sh -file=/home/ubuntu/trasformazioneavm/jobavm.kjb -level=basic -param:processname=avm
if launch shell works if function can see process on system monitor (or shell "top" command) nothing appens (i check memory usage , cpu usage high not in case). body has ideas ?
stdout=subprocess.pipe
here culprit. you're redirecting child process output pipe, aren't reading pipe.
you can either read (if need output process creates), or remove redirection altogether.
edit:
if wish redirect output /dev/null, given fact you're launching command through shell (shell=true
) anyway, simplest solution append > /dev/null
cmd
string. if it's printing stderr, need >/dev/null 2>&1
.
a better solution, doesn't require launching command through shell, follows:
if you're on python 3.3 or higher, pass subprocess.devnull
constant:
subprocess.popen(cmd, stdout=subprocess.devnull, shell=true, preexec_fn=os.setsid)
if not, or want maintain backward compatibility, open /dev/null , pass it:
subprocess.popen(cmd, stdout=open(os.devnull, "w"), shell=true, preexec_fn=os.setsid)
Comments
Post a Comment