09
Sep
11

python subprocess kill example



A little something for me, so that I can remember in the future how to kill a process started via Popen.


import subprocess
import time
import sys

if __name__ == "__main__":
    if len(sys.argv) != 2:
        exit("need an argument")
    to_run = sys.argv[1]
    proc = subprocess.Popen(to_run)
    print "start process with pid %s" % proc.pid
    time.sleep(50)
    if proc.poll() is None:
        print "Killing process %s with pid %s " % (to_run,proc.pid)
        proc.kill()

Advertisements

0 Responses to “python subprocess kill example”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 222,570 hits

%d bloggers like this: