120 likes | 130 Views
Sometimes you may need to get system information. Here is a shell script to help you get CPU and memory utilization. #shellscripting #linux #ubuntu <br><br>Visit https://techimbo.com/shell-script-to-get-cpu-and-memory-utilization/
E N D
Shell script to get CPU and memory utilization
Create empty shell script Open terminal and run the following command to create a blank shell script. $ sudo vi system_stats.sh
Calculate CPU and memory usage #!/bin/bash echo `date` #cpu use threshold cpu_threshold='80' #mem idle threshold mem_threshold='100' #disk use threshold
Calculate CPU and memory usage disk_threshold='90' #---cpu cpu_usage () { cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}'|cut -f 1 -d "."` cpu_use=`expr 100 - $cpu_idle` echo "cpu utilization: $cpu_use"
Calculate CPU and memory usage if [ $cpu_use -gt $cpu_threshold ] then echo "cpu warning!!!" else echo "cpu ok!!!" fi } #---mem
Calculate CPU and memory usage mem_usage () { #MB units mem_free=`free -m | grep "Mem" | awk '{print $4+$6}'` echo "memory space remaining : $mem_free MB" if [ $mem_free -lt $mem_threshold ] then echo "mem warning!!!"
Calculate CPU and memory usage else echo "mem ok!!!" fi } #---disk disk_usage () { disk_use=`df -P | grep /dev | grep -v -E '(tmp|boot)' | awk '{print $5}' | cut -f 1 -d "%"`
Calculate CPU and memory usage echo "disk usage : $disk_use" if [ $disk_use -gt $disk_threshold ] then echo "disk warning!!!" else echo "disk ok!!!"
Calculate CPU and memory usage fi } cpu_usage mem_usage disk_usage
Make Shell Script Executable Run the following command to make shell script executable. $ sudo chmod +x system_stats.sh
Test shell script You can run your shell script using the following command ./system_stats.sh cpu usage : 35% memory space remaining : 3330 MB disk usage : 21%
Thank You Visit for details https://techimbo.com/shell-script-to-get-cpu-and-memory-ut ilization/