-
Notifications
You must be signed in to change notification settings - Fork 2
/
process_memory_stats.sh
executable file
·67 lines (56 loc) · 1.55 KB
/
process_memory_stats.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
59
60
61
62
63
64
65
66
67
#!/bin/bash
# Default values
process_id=""
total_seconds=30
# Parse named arguments
while getopts ":p:t:" opt; do
case $opt in
p)
process_id=$OPTARG
;;
t)
total_seconds=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Check if process ID is provided
if [ -z "$process_id" ]; then
echo "Process ID is required. Usage: $0 -p <process_id> [-t <total_seconds>]"
exit 1
fi
# Variables for calculations
total_memory=0
max_memory=-1
min_memory=-1
count=0
# Run the command for the specified number of seconds
for ((i = 0; i < total_seconds; i++)); do
memory_usage=$(ps -o rss= -p "$process_id" | awk '{print int($1/1024)}')
echo "Memory usage at second $i: ${memory_usage}MB"
# Update variables for calculations
total_memory=$((total_memory + memory_usage))
# Update max and min memory
if [ "$max_memory" -eq -1 ] || [ "$memory_usage" -gt "$max_memory" ]; then
max_memory=$memory_usage
fi
if [ "$min_memory" -eq -1 ] || [ "$memory_usage" -lt "$min_memory" ]; then
min_memory=$memory_usage
fi
sleep 1
count=$((count + 1))
done
# Calculate average memory usage
average_memory=$((total_memory / count))
# Print results
echo ""
echo "Memory Stats for PID $process_id:"
echo "==========================="
echo "min=${min_memory}MB, avg=${average_memory}MB, max=${max_memory}MB"