Search code examples
pythonprocesshandle

Process closing


can I use Popen from python subprocess to close started process? For example, from popen I run some application. In some part of my code I have to close that ran app. For example, from console in Linux I do:

./some_bin
... It works and logs stdout here ...
Ctrl + C and it breaks

I need something like Ctrl + C but in my program code.


Solution

  • from subprocess import Popen
    process = Popen(['slow', 'running', 'program'])
    while process.poll():
        if raw_input() == 'Kill':
            if process.poll(): process.kill()
    

    kill() will kill a process. See more here: Python subprocess module