Shell script to check the memory usage on a Linux server

#!/bin/bash

# Get the total memory available
total_memory=$(free -m | grep 'Mem:' | awk '{print $2}')

# Get the used memory
used_memory=$(free -m | grep 'Mem:' | awk '{print $3}')

# Get the free memory
free_memory=$(free -m | grep 'Mem:' | awk '{print $4}')

# Calculate the percentage of used memory
used_percentage=$(echo "scale=2; $used_memory * 100 / $total_memory" | bc)

# Print the memory usage information

echo "Total Memory: ${total_memory} MB"
echo "Used Memory: ${used_memory} MB"
echo "Free Memory: ${free_memory} MB"
echo "Used Memory Percentage: ${used_percentage}%"

Save the above script in a file, for example, check_memory_usage.sh. Make the script executable using the following command:

chmod +x check_memory_usage.sh

To run the script, simply execute:
./check_memory_usage.sh
Share on Google Plus

About Penguin Technology

I am a passionate cloud and DevOps professional specializing in Linux and open-source solutions. Through this blog, I share my knowledge and experience with the community, offering tips and insights on cloud technologies and DevOps practices.
    Blogger Comment

0 comments:

Post a Comment