简体   繁体   中英

Executing processes with python by subprocess library

i've just created a software in python which should schedule several commands from shell. (ps: i'm working on linux) By the way, i've created the following function to 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 with PID: ', pid 
return pid

After this i use the pid to check if the process goes on zombie's state and in that case a kill it. Sometime everything works just fine but there is a case in which the process launched doesn't do anything. The process i'm talking about is: sh /home/ubuntu/programs/data-integration/kitchen.sh -file=/home/ubuntu/TrasformazioneAVM/JobAVM.kjb -level=Basic -param:processName=avm
If i launch it by shell everything works perfectly but if i do it with my function i can see the process on the system monitor (or by shell with the "top" command) but nothing appens (i check the memory usage and cpu usage which is usually really high but not in this case). Any body has any ideas ?

stdout=subprocess.PIPE

Here is the culprit. You're redirecting your child process output to a pipe, but you aren't reading from that pipe.

You can either read from it (if you need output that process creates), or remove redirection altogether.

Edit:

If you wish to redirect output to /dev/null, given the fact that you're launching your command through shell ( shell=True ) anyway, the simplest solution would be to append > /dev/null to cmd string. If it's also printing to stderr, then you need >/dev/null 2>&1 .

A better solution, that doesn't require launching your command through shell, would be as follows:

If you're on Python 3.3 or higher, just pass subprocess.DEVNULL constant:

subprocess.Popen(cmd, stdout=subprocess.DEVNULL, shell=True, preexec_fn=os.setsid)

If not, or you want to maintain backward compatibility, just open /dev/null and pass it:

subprocess.Popen(cmd, stdout=open(os.devnull, "w"), shell=True, preexec_fn=os.setsid)

Try below

def exec_command(cmd):
    cmd = cmd.split()
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, preexec_fn=os.setsid)
    pid = process.pid
    print '----------launched process with PID: ', pid 
    return pid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM