Listing the top ten CPU– consuming processes in an hour
The CPU is another resource that can be exhausted by a misbehaving process. Linux supports commands to identify and control the processes hogging the CPU.
Getting ready
The ps
command displays details about the processes running on the system. It reports details such as CPU usage, running commands, memory usage, and process status. The ps
command can be used in a script to identify who consumed the most CPU resource over an hour. For more details on the ps
command, refer to Chapter 10, Administration Calls.
How to do it...
This shell script monitors and calculates CPU usages for one hour:
#!/bin/bash #Name: pcpu_usage.sh #Description: Script to calculate cpu usage by processes for 1 hour #Change the SECS to total seconds to monitor CPU usage. #UNIT_TIME is the interval in seconds between each sampling SECS=3600 UNIT_TIME=60 STEPS=$(( $SECS / $UNIT_TIME )) echo Watching CPU usage... ; # Collect data in temp file for((i=0;i<STEPS...