-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysmon
25 lines (20 loc) · 1013 Bytes
/
sysmon
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
#!/bin/bash
# How to Check Memory Usage Per Process on Linux: https://linuxhint.com/check_memory_usage_process_linux/
# @AUTHOR: Shahriar Shovon
printf "%-10s%-15s%-15s%s\n" "PID" "OWNER" "MEMORY" "COMMAND"
sysmon_main() {
# The fisrt command I am going to run will give me the [PID], [OWNER] and [COMMAND] of all the running processes
# separated by colon (:) symbol and store it in the RAWIN variable. Then loop through the output and print it on
# the screen.
RAWIN=$(ps -o pid,user,%mem,command ax | grep -v PID | sort -bnr -k3 | awk '/[0-9]*/{print $1 ":" $2 ":" $4}')
for i in $RAWIN
do
# Delimited information in separate variables.
PID=$(echo $i | cut -d: -f1)
OWNER=$(echo $i | cut -d: -f2)
COMMAND=$(echo $i | cut -d: -f3)
MEMORY=$(sudo pmap $PID | tail -n 1 | awk '/[0-9]K/{print $2}') # Fetch memory usage of each PID.
printf "%-10s%-15s%-15s%s\n" "$PID" "$OWNER" "$MEMORY" "$COMMAND"
done
}
sysmon_main | sort -bnr -k3