Popen not responding to kill
Question: Environment: Raspberry Pi Wheezy

I have a python program that uses Popen to call another python program

    
    
    from subprocess import *
    
    oJob = Popen('sudo python mypgm.py',shell=True)
    

Another menu option is supposed to end the job immediately

    
    
    oJob.kill()
    

but the job is still running??


Answer: You need to add a creation flag arg

    
    
    oJob = Popen('sudo python mypgm.py',shell=True, creationflags = subprocess.CREATE_NEW_PROCESS_GROUP)
    

[source](https://docs.python.org/2/library/subprocess.html#popen-objects)

> subprocess.CREATE_NEW_PROCESS_GROUP A Popen creationflags parameter to
> specify that a new process group will be created. This flag is necessary for
> using os.kill() on the subprocess.

**EDIT** I agree with the comment on how to import stuff and why you are
getting something is undefined. Also the other answer seems to be on the right
track getting the pid

    
    
    import subprocess as sub
    
    oJob = sub.Popen('sudo python mypgm.py', creationflags = sub.CREATE_NEW_PROCESS_GROUP)
    oJob.kill()
    

> Warning Executing shell commands that incorporate unsanitized input from an
> untrusted source makes a program vulnerable to shell injection, a serious
> security flaw which can result in arbitrary command execution. For this
> reason, the use of `shell=True` is strongly discouraged in cases where the
> command string is constructed from external input:

