system icon indicating copy to clipboard operation
system copied to clipboard

Get CPU usage may error

Open Justme0 opened this issue 4 years ago • 0 comments

If proc id is in another process cmd line, grep may find it.

//返回某个进程的cpu使用率
func CpuUsedRateByProc(proc string) string {
	//return ExecOutput("ps axuwww|grep " + proc + "|grep -v grep|awk 'BEGIN{sum=0}{sum+=$3 }END{print sum}'")
	return ExecOutput("top -n 1 -b |grep '"+proc+"'|grep -v grep|awk 'BEGIN{sum=0}{sum+=$9 }END{print sum}'")
}

Justme0 avatar Sep 28 '21 14:09 Justme0