的Python脚本实现功能:启动后自动运行,每秒打印一次用户CPU使用率,脚本结束时自动计算并打印平均值和最大值.#!/usr/bin/python#-*-coding:utf-8-*-importsubprocessimportosimporttimecmd="top-bn1|grep'Cpu(s)'|awk-F'[\"\"%]+''{print$3}'"count=0total=0.0max_use=0.0try:whileTrue:cpu_usage=os.popen(cmd).readline().strip()count=count+1;cpu_use=float(cpu_usage)total=total+cpu_usemax_use=max(max_use,cpu_use)cpu_tip="userCPU:"+cpu_usage+"%"print(cpu_tip)time.sleep(1)exceptKeyboardInterrupt:如果计数!=0:per=total/countprint("userCPUper:"+str(per))print("userCPUmax:"+str(max_use))
