-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmusage
More file actions
executable file
·40 lines (38 loc) · 941 Bytes
/
musage
File metadata and controls
executable file
·40 lines (38 loc) · 941 Bytes
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
#!/usr/bin/env bash
# Print the Resident Set Size memory usage of a process with given PID
# in a human readable format.
#
# Usage:
#
# musage <PID> # take the process by ID
# musage <PROCESS-NAME> # take the newest process that matches the given name
#
# Examples:
#
# musage 12345
# musage firefox
#
# author: andreasl
int_re='^[0-9]+$'
if [[ "$1" =~ $int_re ]]; then
pid="$1"
else
# the subshell containing the `pgrep` below is itself a matching process, filter it out
pids_str="$(pgrep --full "$1")"
mapfile -t pids <<<"$pids_str"
[ "${#pids[@]}" -lt 2 ] && {
>&2 echo "Error: Found no process matching \"${1}\"."
exit 1
}
pid="${pids[-2]}"
fi
# shellcheck disable=SC2016
awk_script='
{
split("KB MB GB TB", units);
for (i = 1; $1 >= 1024 && i; i++) {
$1 = $1 / 1024;
}
printf "%.2f %s\n", $1, units[i];
}'
ps -o rss= -p "$pid" | awk "$awk_script"