1 / 12

Shell Script to Get CPU Memory Utilization

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/

Download Presentation

Shell Script to Get CPU Memory Utilization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shell script to get CPU and memory utilization

  2. Create empty shell script Open terminal and run the following command to create a blank shell script. $ sudo vi system_stats.sh

  3. Calculate CPU and memory usage #!/bin/bash echo `date` #cpu use threshold cpu_threshold='80' #mem idle threshold mem_threshold='100' #disk use threshold

  4. 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"

  5. Calculate CPU and memory usage if [ $cpu_use -gt $cpu_threshold ] then echo "cpu warning!!!" else echo "cpu ok!!!" fi } #---mem

  6. 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!!!"

  7. 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 "%"`

  8. Calculate CPU and memory usage echo "disk usage : $disk_use" if [ $disk_use -gt $disk_threshold ] then echo "disk warning!!!" else echo "disk ok!!!"

  9. Calculate CPU and memory usage fi } cpu_usage mem_usage disk_usage

  10. Make Shell Script Executable Run the following command to make shell script executable. $ sudo chmod +x system_stats.sh

  11. 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%

  12. Thank You Visit for details https://techimbo.com/shell-script-to-get-cpu-and-memory-ut ilization/

More Related