Skip to content

Instantly share code, notes, and snippets.

@ezra100
Created November 3, 2021 14:10
Show Gist options
  • Save ezra100/9d98009ac755ad35d88ab4ae01b09748 to your computer and use it in GitHub Desktop.
Save ezra100/9d98009ac755ad35d88ab4ae01b09748 to your computer and use it in GitHub Desktop.

Linux RAM monitor service

Just a little service I've created to notify when the ram usage is close to it's limits.

To run it just download the files, edit the path in the service file, and then run the following commands:

systemctl --user link /path/to/ram-monitor.service
systemctl --user enable --now ram-monitor.service

#!/bin/bash
# Will notify when ram usage percentage is greater then THREASHOLD_PERCENTAGE
THREASHOLD_PERCENTAGE=95
# Interval between checking ram usage, in seconds
INTERVAL_SECS=5
# gets ram usage as used/total
function getRamUsagePercent(){
free -m | awk '{if ($1 ~ /Mem/) {print int($3/$2*100)}}'
}
while true; do
usage=$(getRamUsagePercent)
if [[ $usage -gt $THREASHOLD_PERCENTAGE ]]; then
remaining=$((100 - usage))
notify-send "Low RAM!" "Only ${remaining}% left!"
fi
sleep $INTERVAL_SECS
done
[Unit]
Description=Notify when RAM is to low
[Service]
Type=simple
# Change this to the location of notify-low-ram.sh
ExecStart=/home/myusername/notify-low-ram.sh
TimeoutStartSec=0
[Install]
WantedBy=default.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment