michael@0: #!/usr/bin/env python michael@0: michael@0: # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: """ michael@0: Kill a process by name. michael@0: """ michael@0: michael@0: import os michael@0: import sys michael@0: import psutil michael@0: michael@0: def main(): michael@0: if len(sys.argv) != 2: michael@0: sys.exit('usage: %s name' % __file__) michael@0: else: michael@0: NAME = sys.argv[1] michael@0: michael@0: killed = [] michael@0: for proc in psutil.process_iter(): michael@0: if proc.name == NAME and proc.pid != os.getpid(): michael@0: proc.kill() michael@0: killed.append(proc.pid) michael@0: if not killed: michael@0: sys.exit('%s: no process found' % NAME) michael@0: else: michael@0: sys.exit(0) michael@0: michael@0: sys.exit(main())