Skip to content

Get CPU usage may error #2

Description

@Justme0

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}'")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions