I have two functions right now:
TUNNEL_COMMAND = "ssh -L 27017:localhost:27017 username@ip -f -N" def kill_tunnel(): p = subprocess.Popen(["ps", "aux"], stdout=subprocess.PIPE) out, err = p.communicate() for line in out.splitlines(): if TUNNEL_COMMAND in line: pid = int(line.split()) print "killing pid", pid os.kill(pid, signal.SIGKILL) def connect_tunnel(): subprocess.Popen(TUNNEL_COMMAND)
connect_tunnel() establishes the connection, which I am ok with, but
kill_tunnel() looks very ugly. Is there a better way to find the PID of a process based off the command?