-
-
Save Royalone94/8b36cc29c6b6957f56e0131447c2690c to your computer and use it in GitHub Desktop.
python kill process by port
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import subprocess | |
import sys | |
import re | |
import os | |
def get_pids(port): | |
command = "sudo lsof -i :%s | awk '{print $2}'" % port | |
pids = subprocess.check_output(command, shell=True) | |
pids = pids.strip() | |
if pids: | |
pids = re.sub(' +', ' ', pids) | |
for pid in pids.split('\n'): | |
try: | |
yield int(pid) | |
except: | |
pass | |
if __name__ == '__main__': | |
args = sys.argv[1:] | |
if not len(args): | |
print 'gimmi port' | |
sys.exit(1) | |
port = args[0] | |
pids = set(get_pids(port)) | |
command = 'sudo kill -9 {}'.format(' '.join([str(pid) for pid in pids])) | |
os.system(command) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment