Python杀死windows进程
import os
import pandas as pd
"""
TCP 192.168.1.155:63758 129.211.126.69:4730 ESTABLISHED 12884
TCP 192.168.1.155:63973 129.211.126.69:4730 ESTABLISHED 3568
"""
def kill_port(port):
find_port = 'netstat -aon | findstr %s' % port
result = os.popen(find_port)
info = result.read().split('\n')
data = []
for line in info:
if not line:
continue
temp = [str for str in line.split(" ") if str]
data.append(temp)
parser_cmd(data)
def parser_cmd(data):
columns = ["type", "secret", "open", "status", "pid"]
df = pd.DataFrame(data=data, columns=list(columns))
for index in range(len(data)):
pid = df.loc[index, 'pid']
kill_pid(pid)
def kill_pid(pid):
find_kill = 'taskkill -f -pid %s' % pid
print(find_kill)
result = os.popen(find_kill)
print(result)
kill_port(4730)
Python3之查看windows下所有进程并杀死指定进程
import psutil
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
# print('pid-%s,pname-%s' % (pid, p.name()))
if p.name() == 'dllhost.exe':
cmd = 'taskkill /F /IM dllhost.exe'
os.system(cmd)
python杀死Windows后台程序
检测 "sogou-gui.exe" 的进程可用tasklist命令
tasklist /FI "IMAGENAME eq sogou-gui.exe"
FI:过滤器 IMAGENAME:映像名称 eq:有效操作符
杀掉 "sogou-gui.exe" 进程可用taskkill命令
taskkill /F /IM sogou-gui.exe
/F:指定强制终止进程 /IM:指定要终止的进程的映像名称,通配符 '*'可用来 指定所有任务或映像名称。
os.system无法获得到输出和返回值,os.popen() 则会返回的 file read 的对象,还有更高级的commands本文就不说了
>>> print os.popen('tasklist /FI "IMAGENAME eq sogou-gui.exe"').read().decode('cp936')标签:exe,python,gui,pid,sogou,window,kill,杀死,os From: https://blog.51cto.com/u_13206712/5839938
映像名称 PID 会话名 会话# 内存使用
========================= ======== ================ =========== ============
sogou-gui.exe 4204 Console 1 15,772 K
>>> "sogou-gui" in os.popen('tasklist /FI "IMAGENAME eq sogou-gui.exe"').read()
True
>>> os.system('TASKKILL /F /IM sogou-gui.exe')
成功: 已终止进程 "sogou-gui.exe",其 PID 为 4204。
0
>>> "sogou-gui" in os.popen('tasklist /FI "IMAGENAME eq sogou-gui.exe"').read()
False