CPU time is an important resource, and sometimes we need to track the process that consumes the most CPU cycles at some time.
In a normal desktop or laptop system, the CPU is in a high-load state and may not cause any problems. But for servers that need to handle a large number of requests, the CPU is an extremely important resource.
By monitoring CPU usage over a period of time, we can identify long-term CPU-intensive processes and optimize them, or debug other related issues.
In Linux systems, the PS command collects detailed information about the processes in the system. This information includes CPU usage, executing commands, memory usage, process status, and so on. Record the process of CPU consumed in one hours, and then use PS and Text processing properly to find out the 10 processes that consume the most CPU.
example, a shell script that monitors and calculates CPU usage within an hour.
#!/bin/bash# file Name: pcpu_usage.sh# Purpose: Calculates the CPU usage of the process within 1 hours secs=3600Unit_time= -# Change the secs to the total number of seconds that need to be monitored # Unit_time is the time interval for sampling, in seconds steps=$ (($SECS/$UNIT _time)) echo watching CPU usage ...; for((i=0; i<steps;i++)) DoPS-eo COMM,PCPU | Tail-n +2>>/tmp/cpu_usage.$$ sleep $UNIT _timedoneecho echo CPU Eaters:cat/tmp/cpu_usage.$$ |awk'{process[$1]+=$2}end{ for(Iinchprocess) {printf ("%-20s%s", I, process[i]; }}'| SORT-NRK 2 | headrm/tmp/cpu_usage.$$ #删除临时日志文件
Output Result:
#./pcpu_usage.sh
Watching CPU usage ...
XOFR 20
Firefox-bin 15
Bash 3
Evince 2
PulseAudio 1.0
pcpu.sh 0.3
Wpa_supplicant 0
Wnck-applet 0
watchdog/0 0
Usb-storage 0
Annotations:
In the above script, the primary input source is Ps-eo Comm,pcpu, where comm represents the command name, and PCPU represents the CPU sample rate (CPU Usae in percent). This command outputs all process names and CPU usage. Each process corresponds to a row of output. Because we need to monitor CPU usage for one hours, we have to keep using Ps-eo comm,pcpu in a cycle of 60 seconds per iteration | Tail-n + Gets the usage statistics for the CPU and adds the data to the file/tmp/cpu_usage.$$. The 60-second iteration time is provided through sleep 60来. This allows the PS to be executed once every minute.
The Tail-n +2 is used to strip the header and command%cpu from the PS output.
The $$ in cpu_usage.$$ represents the process ID of the current script. Assuming the process ID is 1345, it will be replaced with/tmp/cpu_usage.1345 when the script executes. Because this is a temporary file, we put it in the/tmp directory.
After 1 hours, the statistics file is ready, and the file contains 60 items, corresponding to the process state of each minute, respectively.
Then use awk to find out the CPU usage for each process. We used an associative array to count the usage of the CPU. Where the process masterpiece is an array index.
Finally, according to the total CPU usage, the value of the first 10 items is sorted by reverse order and output by head.
jquery Chinese web-original articles, reproduced please specify the source.
Find shell scripts that consume up to 10 CPU processes in 1 hours