dot dot dotfiles
1#!/bin/bash
2
3# A script to debug memory issues i was having. This is intended to be run on a
4# CRON job, and will issue a 'notify-send' warning if memory usage exceeds 50%
5# of available memory.
6
7USER=$(whoami)
8ICON="/usr/share/icons/breeze-dark/status/64/dialog-warning.svg"
9
10# Read MemTotal and MemAvailable from /proc/meminfo
11MemTotal=$(awk '/^MemTotal:/ {print $2}' /proc/meminfo)
12MemAvailable=$(awk '/^MemAvailable:/ {print $2}' /proc/meminfo)
13
14# Convert to integers for safe division
15MemTotal=$((MemTotal / 1024))
16MemAvailable=$((MemAvailable / 1024))
17
18Ratio=$(echo "scale=2; $MemAvailable / $MemTotal" | bc -l)
19RatioPC=$(echo "scale=4; ($MemAvailable / $MemTotal) * 100" | bc -l)
20RatioPC=$(echo "scale=0; $RatioPC" | bc) # round down to int
21RatioPC=$(printf "%.0f%%" "$RatioPC") # convert to string %
22
23if (( $(echo "$Ratio < 0.5" | bc -l) )); then
24 Msg="Less than $RatioPC of memory is available"
25 sudo -u "$USER" DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send --urgency=critical -i $ICON "$Msg"
26fi
27
28# Then with fcron:
29# sudo fcrontab -u <user> -e
30# or to see status
31# sudo fcrontab -u <user> -l
32# to debug fcrontab calls:
33# sudo systemctl status fcron.service