Comments on: Bash script to alert when memory gets low https://blog.al4.co.nz/2010/10/simple-script-to-alert-when-memory-gets-low/ My hobby... Tue, 19 Mar 2019 18:54:54 +0000 hourly 1 https://wordpress.org/?v=6.9 By: Eran https://blog.al4.co.nz/2010/10/simple-script-to-alert-when-memory-gets-low/#comment-13069 Tue, 19 Mar 2019 18:54:54 +0000 http://blog.al4.co.nz/?p=703#comment-13069 I used your code, to build an auto-deploy gist
https://goldmanmalka.com/2019/03/19/bash-script-to-alert-when-memory-gets-low/

]]>
By: Iva https://blog.al4.co.nz/2010/10/simple-script-to-alert-when-memory-gets-low/#comment-12909 Mon, 07 Aug 2017 11:31:52 +0000 http://blog.al4.co.nz/?p=703#comment-12909 In reply to poisonbit.

The http://pastie.org/1264823 is no longer valid link

]]>
By: Alex https://blog.al4.co.nz/2010/10/simple-script-to-alert-when-memory-gets-low/#comment-1094 Sun, 07 Jul 2013 23:40:14 +0000 http://blog.al4.co.nz/?p=703#comment-1094 In reply to kernc.

Wow this seems old now. Cringing at my lack of bash (and Linux) skills.

Kernc, the -o option tells you this, the first column is “%mem”, i.e %age of memory used. Now I would probably do ps -eo %mem,pid,user,args | sort -rn, so that the processes are sorted in order of memory usage. And of course pipe it straight to mail as suggested by poisonbit rather than attach a text file with mutt.

]]>
By: kernc https://blog.al4.co.nz/2010/10/simple-script-to-alert-when-memory-gets-low/#comment-1093 Sun, 07 Jul 2013 23:25:37 +0000 http://blog.al4.co.nz/?p=703#comment-1093 So what do you do once you get the notification? What process is the memory hog?

]]>
By: Hamish https://blog.al4.co.nz/2010/10/simple-script-to-alert-when-memory-gets-low/#comment-91 Sat, 27 Nov 2010 01:23:42 +0000 http://blog.al4.co.nz/?p=703#comment-91 Hey Alex,

I’ve used mutt too, as I don’t know how to use mailutils. However in my server I have a laptop HDD installed in the name of noise, whenever memory is running low and it uses swap, everything slows down a lot, so I have a warning system for when memory is being used.

#!/bin/bash

memfree=”`cat /proc/meminfo | grep MemFree | cut -d: -f2 | cut -dk -f1`”
memfreemb=$(echo “scale=5; $memfree/1024” | bc -l| awk -F. ‘{if(($2/10^length($2)) >= .5) printf(“%d.0n”,$1+1);else printf(“%d.0n”,$1)}’)

if [ $memfree -lt 204800 ]; then
ps -eo %mem,pid,user,args >/tmp/processes.txt
echo ‘Warning, free memory is ‘$memfreemb’mb’ | mutt -a /tmp/processes.txt -s “Server Alert” — [email protected]
fi

]]>
By: Alex https://blog.al4.co.nz/2010/10/simple-script-to-alert-when-memory-gets-low/#comment-90 Mon, 01 Nov 2010 23:10:09 +0000 http://blog.al4.co.nz/?p=703#comment-90 In reply to poisonbit.

Cheers, your version is definitely an improvement!

]]>
By: poisonbit https://blog.al4.co.nz/2010/10/simple-script-to-alert-when-memory-gets-low/#comment-89 Mon, 01 Nov 2010 19:51:50 +0000 http://blog.al4.co.nz/?p=703#comment-89 In reply to poisonbit.

mmm it seems that it is wordpress that eats the code, if you want to edit the first comment, it should look like this: http://pastie.org/1264823

]]>
By: poisonbit https://blog.al4.co.nz/2010/10/simple-script-to-alert-when-memory-gets-low/#comment-88 Mon, 01 Nov 2010 19:49:46 +0000 http://blog.al4.co.nz/?p=703#comment-88 arf, bad editing, little typo corrected:

#!/bin/bash
LANG=C
free=$(free -mt | awk '/Total/{print $4}')
[ $free -lt 256 ] &
]]>
By: poisonbit https://blog.al4.co.nz/2010/10/simple-script-to-alert-when-memory-gets-low/#comment-87 Mon, 01 Nov 2010 19:47:16 +0000 http://blog.al4.co.nz/?p=703#comment-87 Hello,

Just a few tips for the next version:

free -mt | grep Total | awk ‘{print $4}’

is the same that:

free -mt | awk ‘/Total/{print $4}’

Also when I parse commands output, I use to set LANG=C into the script, to avoid locales (“Total” is not said “Total” in all languages)

And in this case, we can avoid the mutt depend to attach files, and insert the txt into the mail body:


#!/bin/bash
LANG=C
free=$(free -mt | awk '/Total/{print $4}')
[ $free -lt 256 ] &mb"
    procs="$(ps -eo %mem,pid,user,args)"
    printf '%sn%s' "$msg" "$procs" | mail -s "Server alert" [email protected]
}

Greetings :)

]]>