Last active
December 23, 2022 16:21
-
-
Save dhenson02/1e6b5934fcef3244fa1576d0cde9c799 to your computer and use it in GitHub Desktop.
Keep me from maxing out RAM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in kB | |
export MINIMUM_AVAILABLE_MEMORY=1400000 | |
if [ ! -e "${NOTIFICATION_SOUND_PATH}" ]; then | |
export NOTIFICATION_SOUND_PATH="/fun/notification-sound-memory-alarm.mp3"; | |
if [ -z "${NOTIFICATION_SOUND_SOURCE}" ]; then | |
export NOTIFICATION_SOUND_SOURCE="https://proxy.notificationsounds.com/wake-up-tones/gentle-alarm-474/download/file-sounds-928-gentle-alarm.mp3"; | |
fi | |
# | |
# This essentially came from: https://askubuntu.com/a/1053890/145918 | |
# | |
wget \ | |
-4 \ | |
-O \ | |
"${NOTIFICATION_SOUND_PATH}" \ | |
"${NOTIFICATION_SOUND_SOURCE}" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
[ -e "${HOME}/.mwrc" ] && . "${HOME}/.mwrc" | |
FIRST_NOTIFIED=0 | |
SECOND_NOTIFIED=0 | |
while true; | |
do | |
# Stored in kB | |
STR=$(grep "MemAvailable" /proc/meminfo) | |
STR_RIGHT="${STR#* }" | |
STR_FINAL="${STR_RIGHT% *}" | |
AVAIL_GB=$((STR_FINAL / (1024.0 * 1024.0))); | |
AVAIL_PRETTY=$(printf "%.1f" "${AVAIL_GB}"); | |
PCT=$((AVAIL_GB / 32.0)); | |
PERCENT_USE=$(((1.0 - PCT) * 100.0)); | |
PCT_PRETTY=$(printf "%.1f" "${PERCENT_USE}"); | |
if [ $STR_FINAL -gt $MINIMUM_AVAILABLE_MEMORY ]; then | |
if [ $SECOND_NOTIFIED -eq 1 ]; then | |
sleep 10; | |
else | |
[ $FIRST_NOTIFIED -eq 1 ] && sleep 15; | |
fi | |
if [ $STR_FINAL -gt $((MINIMUM_AVAILABLE_MEMORY * 3)) ]; then | |
# echo $(date +'%Y-%m-%d %H:%M:%S') - "bigger than 3x" | |
# Available RAM at > 3x minimum | |
FIRST_NOTIFIED=0 | |
SECOND_NOTIFIED=0 | |
sleep 5; | |
continue; | |
else | |
FIRST_NOTIFIED=1 | |
[ $STR_FINAL -gt $((MINIMUM_AVAILABLE_MEMORY * 2)) ] || SECOND_NOTIFIED=1; | |
fi | |
if [ $SECOND_NOTIFIED -eq 1 ]; then | |
# < 2x minimum remaining | |
echo $(date +'%Y-%m-%d %H:%M:%S') - "less than 2x" | |
notify-send \ | |
--urgency=normal \ | |
-i "info" \ | |
"LOW MEMORY" \ | |
"${AVAIL_PRETTY}GB available (${PCT_PRETTY}% used)"; | |
play -q -v 0.50 "${NOTIFICATION_SOUND_PATH}"; | |
play -q -v 0.75 "${NOTIFICATION_SOUND_PATH}"; | |
sleep 8; | |
else | |
# Available RAM at > 2x minimum | |
# echo $(date +'%Y-%m-%d %H:%M:%S') - "bigger than 2x" | |
SECOND_NOTIFIED=0 | |
if [ $FIRST_NOTIFIED -eq 1 ]; then | |
# ... but < 3x minimum | |
echo $(date +'%Y-%m-%d %H:%M:%S') - "less than 3x" | |
notify-send \ | |
--urgency=low \ | |
-i "computer-fail" \ | |
"Memory usage increased" \ | |
"${AVAIL_PRETTY}GB available (${PCT_PRETTY}% used)"; | |
# play -q -v 0.025 "${NOTIFICATION_SOUND_PATH}"; | |
sleep 40; | |
else | |
FIRST_NOTIFIED=0 | |
continue; | |
fi | |
fi | |
sleep 3; | |
else | |
FIRST_NOTIFIED=0 | |
SECOND_NOTIFIED=0 | |
echo $(date +'%Y-%m-%d %H:%M:%S') - "less than 1x" | |
>&2 echo $(date +'%Y-%m-%d %H:%M:%S') - "[CRASH IMMENENT] ${AVAIL_PRETTY}GB available (${PCT_PRETTY}% used)"; | |
notify-send \ | |
--urgency=critical \ | |
-i "error" \ | |
"LOW MEMORY, CRASH IMMENENT" \ | |
"${AVAIL_PRETTY}GB available (${PCT_PRETTY}% used)"; | |
play -q -v 1.0 "${NOTIFICATION_SOUND_PATH}"; | |
play -q -v 1.0 "${NOTIFICATION_SOUND_PATH}"; | |
play -q -v 1.0 "${NOTIFICATION_SOUND_PATH}"; | |
play -q -v 1.0 "${NOTIFICATION_SOUND_PATH}"; | |
play -q -v 1.0 "${NOTIFICATION_SOUND_PATH}"; | |
sleep 6; | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment