Get the CPU usage within the specified time

Get the command name, remove the first line of cpu ratio and add it to cpu_usage.$$

ps -eocomm,pcpu | tail -n +2 >> cpu_usage.$$

Create a process array in awk, use the command name as the key, the cpu ratio as the value, and then superimpose the value

Use sort to reverse the second column to sort the first ten rows by key.

cat cpu_usage.$$ | awk '{process[$1]+=$2;} END{for(i in process){printf("%-20s %s\n",i,process[i]);}}' \
| sort -nrk 2 | head >> `date +%F`.cpu_usage 

awk's if for is in a c-like format

The same can be obtained

ps -eo comm,pmem get memory usage

ps -eo comm,user,tty,nice get the user who started the process, associated tty, priority

pgrep command to get the pid of the command

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325266594&siteId=291194637