python解决window的进程powershell.exe占用cpu使用率过高
背景
window的进程powershell.exe占用cpu使用率过高,时常导致计算机卡顿,于是写了一个python的脚本监测这个脚本,当cpu使用到达了40%以上,直接终结这个进程。
依赖
这里用的库是psutil
,这是一个跨平台的进程和系统工具,能够方便我们对cpu和内存等监测
pip install pipenv
pipenv install psutil
解决方法
脚本为循环任务,间隔为10s
import psutil
import time
while True:
for proc in psutil.process_iter(attrs=['pid', 'name']):
if proc.name() == 'powershell.exe':
cpu_percent = proc.cpu_percent()
print('current cpu percent: %s' % str(cpu_percent))
if cpu_percent > 40:
proc.terminate()
print('powershell.exe has been terminate')
time.sleep(5)

0 comments
To reply to the article, please Login or registered