# check memory and restart service when less than X gb.

bash  
\#!/bin/bash

\# Get the available memory in KB  
free\_mem\_kb=$(free -k | grep Mem | awk '{print $4}')

\# Convert to GB (divide by 1024^2)  
\# without bc  
free\_mem\_gb=$((free\_mem\_kb / 1024 / 1024))  
\# with bc  
\# free\_mem\_gb=$(echo "scale=2; $free\_mem\_kb / (1024 \* 1024)" | bc)

\# Threshold for memory in GB  
threshold=2

\# Check if free memory is less than the threshold  
\# without bc  
if \[ "$free\_mem\_gb" -lt "$threshold" \]; then  
\# with bc  
\# if (( $(echo "$free\_mem\_gb &lt; $threshold" | bc -l) )); then  
 echo "Free memory is less than 2GB ($free\_mem\_gb GB). Restarting Apache..."  
\# restart some service here  
\# sudo systemctl restart apache2  
else  
 echo "Free memory is sufficient: $free\_mem\_gb GB."  
fi