From 0de58b48950f67731792935264bffa91f5ceeeb9 Mon Sep 17 00:00:00 2001 From: Zioguillo Date: Wed, 22 Sep 2021 14:56:02 -0700 Subject: [PATCH 1/2] adding bash codes --- bash/monitor_cpu_usage.sh | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 bash/monitor_cpu_usage.sh diff --git a/bash/monitor_cpu_usage.sh b/bash/monitor_cpu_usage.sh new file mode 100644 index 0000000..0b00df0 --- /dev/null +++ b/bash/monitor_cpu_usage.sh @@ -0,0 +1,45 @@ +#!/bin/bash +#Name: monitor_cpu_usage.sh +#Description: Script to check top cpu consuming process for 1 hour + +#Change the SECS to total seconds to monitor CPU usage. +#UNIT_TIME is the interval in seconds between each sampling + + +function report_utilisation { + # Process collected data + echo + echo "CPU eaters :" + + cat /tmp/cpu_usage.$$ | +awk ' +{ process[$1]+=$2; } +END{ + for(i in process) + { + printf("%-20s %s\n",i, process[i]) ; + } + + }' | sort -nrk 2 | head + +#Remove the temporary log file +rm /tmp/cpu_usage.$$ +exit 0 +} + +trap 'report_utilisation' INT + +SECS=3600 +UNIT_TIME=10 + +STEPS=$(( $SECS / $UNIT_TIME )) + +echo "Watching CPU usage... ;" + +# Collect data in temp file +for((i=0;i<$STEPS;i++)); do + ps -eocomm,pcpu | egrep -v '(0.0)|(%CPU)' >> /tmp/cpu_usage.$$ + sleep $UNIT_TIME +done + +report_utilisation \ No newline at end of file From d59d74697bb576621419bedbad1022486951d2f5 Mon Sep 17 00:00:00 2001 From: Zioguillo Date: Fri, 1 Oct 2021 09:44:24 -0700 Subject: [PATCH 2/2] . --- bash/monitor_cpu_usage.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash/monitor_cpu_usage.sh b/bash/monitor_cpu_usage.sh index 0b00df0..7e4d452 100644 --- a/bash/monitor_cpu_usage.sh +++ b/bash/monitor_cpu_usage.sh @@ -5,7 +5,7 @@ #Change the SECS to total seconds to monitor CPU usage. #UNIT_TIME is the interval in seconds between each sampling - + function report_utilisation { # Process collected data echo