-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_mem.sh
executable file
·58 lines (43 loc) · 1.07 KB
/
check_mem.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
set -e
interval=1 # N of seconds between memory queries
record_per_process=0
min_memory=0
usage=$"$(basename "$0") [-h] [-t n] [-p] \n
where: \n
-h show this help text \n
-t sampling interval in seconds. Must be an integer \n
-p record memory per user process. \n
-m entries with memory below this thresold are ignored. Units in kilobites
"
while getopts m:pt:h flag
do
case "${flag}" in
t) interval=${OPTARG};;
h) echo -e $usage 1>&2
exit 1
;;
p) record_per_process=1 ;;
m) min_memory=${OPTARG}
esac
done
if [ $record_per_process == 1 ]
then
echo "Step Memory Pid Name"
else
echo "Step Memory"
fi
i=1
while [ 1 ]
do
if [ $record_per_process == 1 ]
then
# saves memory usage per process from ps
ps -F | tail -n +2 | awk "{ if (\$6 > $min_memory) print $i,\$6,\$2,\$11 }"
else
# saves system wide memory usage
free --kilo | grep Mem | awk "{if (\$3 > $min_memory) print $i,\$3}"
fi
sleep $interval
i=$((i+interval))
done