Sorting down processes by memory usage
I am able to see the list of all the processes and the memory via
ps aux
and going through the VSZ and RSS
Is there a way to sort down the output of this command by the descending order on RSS value?
linux memory
add a comment |
I am able to see the list of all the processes and the memory via
ps aux
and going through the VSZ and RSS
Is there a way to sort down the output of this command by the descending order on RSS value?
linux memory
What would be the expected output ? checkps
– Rahul Patil
Sep 26 '13 at 14:40
Additional examples of how to use--sort
are here: alvinalexander.com/linux/…
– slm♦
Sep 26 '13 at 15:16
1
Superset question, using any tool: stackoverflow.com/questions/4802481/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
May 21 '14 at 16:12
add a comment |
I am able to see the list of all the processes and the memory via
ps aux
and going through the VSZ and RSS
Is there a way to sort down the output of this command by the descending order on RSS value?
linux memory
I am able to see the list of all the processes and the memory via
ps aux
and going through the VSZ and RSS
Is there a way to sort down the output of this command by the descending order on RSS value?
linux memory
linux memory
edited Oct 2 at 12:29
7ochem
141110
141110
asked Sep 26 '13 at 14:35
user2817836
643264
643264
What would be the expected output ? checkps
– Rahul Patil
Sep 26 '13 at 14:40
Additional examples of how to use--sort
are here: alvinalexander.com/linux/…
– slm♦
Sep 26 '13 at 15:16
1
Superset question, using any tool: stackoverflow.com/questions/4802481/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
May 21 '14 at 16:12
add a comment |
What would be the expected output ? checkps
– Rahul Patil
Sep 26 '13 at 14:40
Additional examples of how to use--sort
are here: alvinalexander.com/linux/…
– slm♦
Sep 26 '13 at 15:16
1
Superset question, using any tool: stackoverflow.com/questions/4802481/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
May 21 '14 at 16:12
What would be the expected output ? check
ps
– Rahul Patil
Sep 26 '13 at 14:40
What would be the expected output ? check
ps
– Rahul Patil
Sep 26 '13 at 14:40
Additional examples of how to use
--sort
are here: alvinalexander.com/linux/…– slm♦
Sep 26 '13 at 15:16
Additional examples of how to use
--sort
are here: alvinalexander.com/linux/…– slm♦
Sep 26 '13 at 15:16
1
1
Superset question, using any tool: stackoverflow.com/questions/4802481/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
May 21 '14 at 16:12
Superset question, using any tool: stackoverflow.com/questions/4802481/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
May 21 '14 at 16:12
add a comment |
7 Answers
7
active
oldest
votes
Use the following command:
ps aux --sort -rss
Check here for more Linux process memory usage
12
note - if you want to see the top results it's helpful to pipe it intohead
likeps aux --sort -rss | head -n15
– Yehosef
Mar 26 '15 at 15:38
3
I get errorps: illegal option -- -
– Miguel Mota
May 19 '16 at 18:27
@MiguelMota What if like this:ps aux --sort=rss
?
– coffeMug
May 19 '16 at 18:32
2
@coffeMug didn't work but this didps aux | sort -rn -k 6
– Miguel Mota
May 19 '16 at 18:41
1
@coffeMug not sure how to find the version but I'm on Mac OSX so maybe that's why
– Miguel Mota
May 19 '16 at 18:58
|
show 4 more comments
A quick and dirty method is to just pipe the output of ps aux
to the sort
command:
$ ps aux | sort -rn -k 5,6
Example
$ ps aux | sort -rn -k 5,6
...
root 1584 0.0 0.0 22540 1236 ? S 07:04 0:01 hald-addon-storage: polling /dev/sr0 (every 2 sec)
root 1575 0.0 0.0 22536 872 ? S 07:04 0:00 /usr/libexec/hald-addon-generic-backlight
root 1574 0.0 0.0 22536 880 ? S 07:04 0:00 /usr/libexec/hald-addon-leds
root 1565 0.0 0.0 22536 876 ? S 07:04 0:00 /usr/libexec/hald-addon-rfkill-killswitch
saml 2507 0.0 0.0 22232 500 ? S 07:05 0:00 dbus-launch --sh-syntax --exit-with-session
root 1671 0.0 0.0 22156 936 ? Ss 07:04 0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
...
This doesn't handle for the column headers which get mixed in with the output, but it's easy to remember on the command line, and is an acceptable way to do what you want when manually viewing this type of output.
Example
root 1791 0.0 0.0 4140 536 tty2 Ss+ 07:04 0:00 /sbin/mingetty /dev/tty2
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 996 0.0 0.0 0 0 ? S 07:04 0:01 [kdmflush]
root 982 0.0 0.0 0 0 ? S 07:04 0:00 [kvm-irqfd-clean]
More tips
An additional tip would be to pipe the entire output to another command such as less
. This allows you to look at the information a page at a time and also use the arrow keys and page up/down keys to scroll back and forth through the output.
$ ps aux | sort -rn -k 5,6 | less
If your output is wrapping a lot you can also utilize the -S
switch to less, which will force all the output to stay on a single line instead. You can then use your arrow keys to move left/right/up/down to see all of it.
$ ps aux | sort -rn -k 5,6 | less -S
Sorting within ps
Certain versions of ps
provide the ability to use --sort
. This switch can then take keys that are either prefixed with a +
or a -
to denote the sort order...least to greatest or greatest to least.
Examples
vsz,-rss
$ ps aux --sort=vsz,-rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 07:03 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:03 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 07:03 0:01 [migration/0]
root 5 0.0 0.0 0 0 ? S 07:03 0:00 [watchdog/0]
+vsz,+rss
$ ps aux --sort=+vsz,+rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 07:03 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:03 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 07:03 0:01 [migration/0]
root 5 0.0 0.0 0 0 ? S 07:03 0:00 [watchdog/0]
-vsz,-rss
$ ps aux --sort=-vsz,-rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1832 0.0 0.0 2088924 3312 ? Sl 07:04 0:00 /usr/sbin/console-kit-daemon --no-daemon
saml 3517 0.2 1.2 2073196 100492 ? Sl 07:06 0:34 /home/saml/.dropbox-dist/dropbox
saml 3516 0.0 0.8 2071032 67388 ? Sl 07:06 0:07 /home/saml/.dropbox-dist/dropbox
saml 2657 0.1 0.7 1580936 57788 ? Sl 07:05 0:27 nautilus
willps
always output the columns in the way you expectsort
to see/process them?
– Felipe Alvarez
Aug 13 '15 at 2:34
Depends on which version of ps
– slm♦
Aug 13 '15 at 4:04
2
A... | less
is a good advice but sometimes your process has a huge command line and it clutters the output. In such cases... | less -S
works better.
– waste
Nov 17 '16 at 23:54
@waste - good tip, just remember that-S
truncates and so you may lose some of what you want to see, but otherwise good advice if you're only interested the left most columns.
– slm♦
Nov 18 '16 at 2:54
@slm I am not sure that is the case forless -S
. When you closeless
view everything disappears, but as long as you are in the view, you can scroll vertically but also horizontally. Copying might be difficult, though.
– waste
Nov 18 '16 at 3:03
|
show 2 more comments
Even if ps do not reflect the actual memory used, this command is pretty helpful.
ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }'
add a comment |
simple way is to install htop
in that you can sort process based on PID,Percentage CPU,MEM
more sophisticated
add a comment |
As an alternative to the BSD style arguments shown in the other answers, one can use (at least using procps, shipped by Debian and Ubuntu):
ps -eF --sort=-rss
add a comment |
- Run
top
command
Shift + F
to sort based on field (see the full menu below)- Select
n
to sort based on memory usage
n: %MEM = Memory usage (RES)
add a comment |
ps aux --sort -rss is nice:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 5984 0.8 7.4 1632488 296056 ? Sl 06:30 6:18 /usr/lib/chromium-browser/chromium-browser --type=ren
user 23934 21.7 6.0 1565600 241228 ? Sl 15:45 40:10 /opt/atom/atom --type=renderer --enable-experimental-
user 5533 0.9 5.1 3154096 206376 ? SLl 06:30 6:47 /usr/lib/chromium-browser/chromium-browser --enable-p
user 17306 1.7 4.9 1360648 196124 ? Sl 18:14 0:36 /usr/lib/chromium-browser/chromium-browser --type=ren
user 22272 30.1 4.6 1347784 185032 ? Sl 18:43 1:54 /usr/lib/chromium-browser/chromium-browser --type=ren
user 19318 0.6 3.3 1304324 133452 ? Sl 18:27 0:09 /usr/lib/chromium-browser/chromium-browser --type=ren
user 22098 1.0 3.3 1298500 133216 ? Sl 18:43 0:04 /usr/lib/chromium-browser/chromium-browser --type=ren
but if you want to see memory and cpu usages by application (grouped by commands):
python3.6 sum_process_resources.py
==== CPU% ====
0. /opt/atom/atom | 27.8
1. /usr/lib/chromium-browser/chromium-browser | 11.2
2. python3.6 | 11.0
3. /opt/google/chrome/chrome | 1.6
4. /usr/lib/xorg/Xorg | 1.4
5. /opt/Franz/franz | 0.7
==== MEM% ====
0. /usr/lib/chromium-browser/chromium-browser | 37.2
1. /opt/google/chrome/chrome | 11.3
2. /opt/Franz/franz | 10.6
3. /opt/atom/atom | 10.1
4. /usr/lib/xorg/Xorg | 2.0
5. com.google.android.gms.persistent | 1.4
==== RSS MB ====
0. /usr/lib/chromium-browser/chromium-browser | 1475.07 MB
1. /opt/google/chrome/chrome | 461.35 MB
2. /opt/Franz/franz | 429.04 MB
3. /opt/atom/atom | 402.18 MB
4. /usr/lib/xorg/Xorg | 78.53 MB
5. com.google.android.gms.persistent | 58.02 MB
code:
#sum_process_resources.py
from collections import OrderedDict
import subprocess
def run_cmd(cmd_string):
"""Runs commands and saves output to variable"""
cmd_list = cmd_string.split(" ")
popen_obj = subprocess.Popen(cmd_list, stdout=subprocess.PIPE)
output = popen_obj.stdout.read()
output = output.decode("utf8")
return output
def sum_process_resources():
"""Sums top X cpu and memory usages grouped by processes"""
ps_memory, ps_cpu, ps_rss = {}, {}, {}
top = 6
output = run_cmd('ps aux').split("n")
for i, line in enumerate(output):
cleaned_list = " ".join(line.split())
line_list = cleaned_list.split(" ")
if i > 0 and len(line_list) > 10:
cpu = float(line_list[2])
memory = float(line_list[3])
rss = float(line_list[5])
command = line_list[10]
ps_cpu[command] = round(ps_cpu.get(command, 0) + cpu, 2)
ps_memory[command] = round(ps_memory.get(command, 0) + memory, 2)
ps_rss[command] = round(ps_rss.get(command, 0) + rss, 2)
sorted_cpu = OrderedDict(sorted(ps_cpu.items(), key=lambda x: x[1], reverse=True))
sorted_memory = OrderedDict(sorted(ps_memory.items(), key=lambda x: x[1], reverse=True))
sorted_rss = OrderedDict(sorted(ps_rss.items(), key=lambda x: x[1], reverse=True))
print("==== CPU% ====")
for i, k in enumerate(sorted_cpu.items()):
if i < top:
print("{}. {} | {}".format(i, k[0], k[1]))
print("==== MEM% ====")
for i, k in enumerate(sorted_memory.items()):
if i < top:
print("{}. {} | {}".format(i, k[0], k[1]))
print("==== RSS MB ====")
for i, k in enumerate(sorted_rss.items()):
if i < top:
print("{}. {} | {} MB".format(i, k[0], round((k[1]/1024), 2)))
if __name__ == '__main__':
sum_process_resources()
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f92493%2fsorting-down-processes-by-memory-usage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the following command:
ps aux --sort -rss
Check here for more Linux process memory usage
12
note - if you want to see the top results it's helpful to pipe it intohead
likeps aux --sort -rss | head -n15
– Yehosef
Mar 26 '15 at 15:38
3
I get errorps: illegal option -- -
– Miguel Mota
May 19 '16 at 18:27
@MiguelMota What if like this:ps aux --sort=rss
?
– coffeMug
May 19 '16 at 18:32
2
@coffeMug didn't work but this didps aux | sort -rn -k 6
– Miguel Mota
May 19 '16 at 18:41
1
@coffeMug not sure how to find the version but I'm on Mac OSX so maybe that's why
– Miguel Mota
May 19 '16 at 18:58
|
show 4 more comments
Use the following command:
ps aux --sort -rss
Check here for more Linux process memory usage
12
note - if you want to see the top results it's helpful to pipe it intohead
likeps aux --sort -rss | head -n15
– Yehosef
Mar 26 '15 at 15:38
3
I get errorps: illegal option -- -
– Miguel Mota
May 19 '16 at 18:27
@MiguelMota What if like this:ps aux --sort=rss
?
– coffeMug
May 19 '16 at 18:32
2
@coffeMug didn't work but this didps aux | sort -rn -k 6
– Miguel Mota
May 19 '16 at 18:41
1
@coffeMug not sure how to find the version but I'm on Mac OSX so maybe that's why
– Miguel Mota
May 19 '16 at 18:58
|
show 4 more comments
Use the following command:
ps aux --sort -rss
Check here for more Linux process memory usage
Use the following command:
ps aux --sort -rss
Check here for more Linux process memory usage
edited Sep 26 '13 at 15:12
answered Sep 26 '13 at 15:02
coffeMug
7,043102646
7,043102646
12
note - if you want to see the top results it's helpful to pipe it intohead
likeps aux --sort -rss | head -n15
– Yehosef
Mar 26 '15 at 15:38
3
I get errorps: illegal option -- -
– Miguel Mota
May 19 '16 at 18:27
@MiguelMota What if like this:ps aux --sort=rss
?
– coffeMug
May 19 '16 at 18:32
2
@coffeMug didn't work but this didps aux | sort -rn -k 6
– Miguel Mota
May 19 '16 at 18:41
1
@coffeMug not sure how to find the version but I'm on Mac OSX so maybe that's why
– Miguel Mota
May 19 '16 at 18:58
|
show 4 more comments
12
note - if you want to see the top results it's helpful to pipe it intohead
likeps aux --sort -rss | head -n15
– Yehosef
Mar 26 '15 at 15:38
3
I get errorps: illegal option -- -
– Miguel Mota
May 19 '16 at 18:27
@MiguelMota What if like this:ps aux --sort=rss
?
– coffeMug
May 19 '16 at 18:32
2
@coffeMug didn't work but this didps aux | sort -rn -k 6
– Miguel Mota
May 19 '16 at 18:41
1
@coffeMug not sure how to find the version but I'm on Mac OSX so maybe that's why
– Miguel Mota
May 19 '16 at 18:58
12
12
note - if you want to see the top results it's helpful to pipe it into
head
like ps aux --sort -rss | head -n15
– Yehosef
Mar 26 '15 at 15:38
note - if you want to see the top results it's helpful to pipe it into
head
like ps aux --sort -rss | head -n15
– Yehosef
Mar 26 '15 at 15:38
3
3
I get error
ps: illegal option -- -
– Miguel Mota
May 19 '16 at 18:27
I get error
ps: illegal option -- -
– Miguel Mota
May 19 '16 at 18:27
@MiguelMota What if like this:
ps aux --sort=rss
?– coffeMug
May 19 '16 at 18:32
@MiguelMota What if like this:
ps aux --sort=rss
?– coffeMug
May 19 '16 at 18:32
2
2
@coffeMug didn't work but this did
ps aux | sort -rn -k 6
– Miguel Mota
May 19 '16 at 18:41
@coffeMug didn't work but this did
ps aux | sort -rn -k 6
– Miguel Mota
May 19 '16 at 18:41
1
1
@coffeMug not sure how to find the version but I'm on Mac OSX so maybe that's why
– Miguel Mota
May 19 '16 at 18:58
@coffeMug not sure how to find the version but I'm on Mac OSX so maybe that's why
– Miguel Mota
May 19 '16 at 18:58
|
show 4 more comments
A quick and dirty method is to just pipe the output of ps aux
to the sort
command:
$ ps aux | sort -rn -k 5,6
Example
$ ps aux | sort -rn -k 5,6
...
root 1584 0.0 0.0 22540 1236 ? S 07:04 0:01 hald-addon-storage: polling /dev/sr0 (every 2 sec)
root 1575 0.0 0.0 22536 872 ? S 07:04 0:00 /usr/libexec/hald-addon-generic-backlight
root 1574 0.0 0.0 22536 880 ? S 07:04 0:00 /usr/libexec/hald-addon-leds
root 1565 0.0 0.0 22536 876 ? S 07:04 0:00 /usr/libexec/hald-addon-rfkill-killswitch
saml 2507 0.0 0.0 22232 500 ? S 07:05 0:00 dbus-launch --sh-syntax --exit-with-session
root 1671 0.0 0.0 22156 936 ? Ss 07:04 0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
...
This doesn't handle for the column headers which get mixed in with the output, but it's easy to remember on the command line, and is an acceptable way to do what you want when manually viewing this type of output.
Example
root 1791 0.0 0.0 4140 536 tty2 Ss+ 07:04 0:00 /sbin/mingetty /dev/tty2
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 996 0.0 0.0 0 0 ? S 07:04 0:01 [kdmflush]
root 982 0.0 0.0 0 0 ? S 07:04 0:00 [kvm-irqfd-clean]
More tips
An additional tip would be to pipe the entire output to another command such as less
. This allows you to look at the information a page at a time and also use the arrow keys and page up/down keys to scroll back and forth through the output.
$ ps aux | sort -rn -k 5,6 | less
If your output is wrapping a lot you can also utilize the -S
switch to less, which will force all the output to stay on a single line instead. You can then use your arrow keys to move left/right/up/down to see all of it.
$ ps aux | sort -rn -k 5,6 | less -S
Sorting within ps
Certain versions of ps
provide the ability to use --sort
. This switch can then take keys that are either prefixed with a +
or a -
to denote the sort order...least to greatest or greatest to least.
Examples
vsz,-rss
$ ps aux --sort=vsz,-rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 07:03 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:03 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 07:03 0:01 [migration/0]
root 5 0.0 0.0 0 0 ? S 07:03 0:00 [watchdog/0]
+vsz,+rss
$ ps aux --sort=+vsz,+rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 07:03 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:03 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 07:03 0:01 [migration/0]
root 5 0.0 0.0 0 0 ? S 07:03 0:00 [watchdog/0]
-vsz,-rss
$ ps aux --sort=-vsz,-rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1832 0.0 0.0 2088924 3312 ? Sl 07:04 0:00 /usr/sbin/console-kit-daemon --no-daemon
saml 3517 0.2 1.2 2073196 100492 ? Sl 07:06 0:34 /home/saml/.dropbox-dist/dropbox
saml 3516 0.0 0.8 2071032 67388 ? Sl 07:06 0:07 /home/saml/.dropbox-dist/dropbox
saml 2657 0.1 0.7 1580936 57788 ? Sl 07:05 0:27 nautilus
willps
always output the columns in the way you expectsort
to see/process them?
– Felipe Alvarez
Aug 13 '15 at 2:34
Depends on which version of ps
– slm♦
Aug 13 '15 at 4:04
2
A... | less
is a good advice but sometimes your process has a huge command line and it clutters the output. In such cases... | less -S
works better.
– waste
Nov 17 '16 at 23:54
@waste - good tip, just remember that-S
truncates and so you may lose some of what you want to see, but otherwise good advice if you're only interested the left most columns.
– slm♦
Nov 18 '16 at 2:54
@slm I am not sure that is the case forless -S
. When you closeless
view everything disappears, but as long as you are in the view, you can scroll vertically but also horizontally. Copying might be difficult, though.
– waste
Nov 18 '16 at 3:03
|
show 2 more comments
A quick and dirty method is to just pipe the output of ps aux
to the sort
command:
$ ps aux | sort -rn -k 5,6
Example
$ ps aux | sort -rn -k 5,6
...
root 1584 0.0 0.0 22540 1236 ? S 07:04 0:01 hald-addon-storage: polling /dev/sr0 (every 2 sec)
root 1575 0.0 0.0 22536 872 ? S 07:04 0:00 /usr/libexec/hald-addon-generic-backlight
root 1574 0.0 0.0 22536 880 ? S 07:04 0:00 /usr/libexec/hald-addon-leds
root 1565 0.0 0.0 22536 876 ? S 07:04 0:00 /usr/libexec/hald-addon-rfkill-killswitch
saml 2507 0.0 0.0 22232 500 ? S 07:05 0:00 dbus-launch --sh-syntax --exit-with-session
root 1671 0.0 0.0 22156 936 ? Ss 07:04 0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
...
This doesn't handle for the column headers which get mixed in with the output, but it's easy to remember on the command line, and is an acceptable way to do what you want when manually viewing this type of output.
Example
root 1791 0.0 0.0 4140 536 tty2 Ss+ 07:04 0:00 /sbin/mingetty /dev/tty2
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 996 0.0 0.0 0 0 ? S 07:04 0:01 [kdmflush]
root 982 0.0 0.0 0 0 ? S 07:04 0:00 [kvm-irqfd-clean]
More tips
An additional tip would be to pipe the entire output to another command such as less
. This allows you to look at the information a page at a time and also use the arrow keys and page up/down keys to scroll back and forth through the output.
$ ps aux | sort -rn -k 5,6 | less
If your output is wrapping a lot you can also utilize the -S
switch to less, which will force all the output to stay on a single line instead. You can then use your arrow keys to move left/right/up/down to see all of it.
$ ps aux | sort -rn -k 5,6 | less -S
Sorting within ps
Certain versions of ps
provide the ability to use --sort
. This switch can then take keys that are either prefixed with a +
or a -
to denote the sort order...least to greatest or greatest to least.
Examples
vsz,-rss
$ ps aux --sort=vsz,-rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 07:03 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:03 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 07:03 0:01 [migration/0]
root 5 0.0 0.0 0 0 ? S 07:03 0:00 [watchdog/0]
+vsz,+rss
$ ps aux --sort=+vsz,+rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 07:03 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:03 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 07:03 0:01 [migration/0]
root 5 0.0 0.0 0 0 ? S 07:03 0:00 [watchdog/0]
-vsz,-rss
$ ps aux --sort=-vsz,-rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1832 0.0 0.0 2088924 3312 ? Sl 07:04 0:00 /usr/sbin/console-kit-daemon --no-daemon
saml 3517 0.2 1.2 2073196 100492 ? Sl 07:06 0:34 /home/saml/.dropbox-dist/dropbox
saml 3516 0.0 0.8 2071032 67388 ? Sl 07:06 0:07 /home/saml/.dropbox-dist/dropbox
saml 2657 0.1 0.7 1580936 57788 ? Sl 07:05 0:27 nautilus
willps
always output the columns in the way you expectsort
to see/process them?
– Felipe Alvarez
Aug 13 '15 at 2:34
Depends on which version of ps
– slm♦
Aug 13 '15 at 4:04
2
A... | less
is a good advice but sometimes your process has a huge command line and it clutters the output. In such cases... | less -S
works better.
– waste
Nov 17 '16 at 23:54
@waste - good tip, just remember that-S
truncates and so you may lose some of what you want to see, but otherwise good advice if you're only interested the left most columns.
– slm♦
Nov 18 '16 at 2:54
@slm I am not sure that is the case forless -S
. When you closeless
view everything disappears, but as long as you are in the view, you can scroll vertically but also horizontally. Copying might be difficult, though.
– waste
Nov 18 '16 at 3:03
|
show 2 more comments
A quick and dirty method is to just pipe the output of ps aux
to the sort
command:
$ ps aux | sort -rn -k 5,6
Example
$ ps aux | sort -rn -k 5,6
...
root 1584 0.0 0.0 22540 1236 ? S 07:04 0:01 hald-addon-storage: polling /dev/sr0 (every 2 sec)
root 1575 0.0 0.0 22536 872 ? S 07:04 0:00 /usr/libexec/hald-addon-generic-backlight
root 1574 0.0 0.0 22536 880 ? S 07:04 0:00 /usr/libexec/hald-addon-leds
root 1565 0.0 0.0 22536 876 ? S 07:04 0:00 /usr/libexec/hald-addon-rfkill-killswitch
saml 2507 0.0 0.0 22232 500 ? S 07:05 0:00 dbus-launch --sh-syntax --exit-with-session
root 1671 0.0 0.0 22156 936 ? Ss 07:04 0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
...
This doesn't handle for the column headers which get mixed in with the output, but it's easy to remember on the command line, and is an acceptable way to do what you want when manually viewing this type of output.
Example
root 1791 0.0 0.0 4140 536 tty2 Ss+ 07:04 0:00 /sbin/mingetty /dev/tty2
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 996 0.0 0.0 0 0 ? S 07:04 0:01 [kdmflush]
root 982 0.0 0.0 0 0 ? S 07:04 0:00 [kvm-irqfd-clean]
More tips
An additional tip would be to pipe the entire output to another command such as less
. This allows you to look at the information a page at a time and also use the arrow keys and page up/down keys to scroll back and forth through the output.
$ ps aux | sort -rn -k 5,6 | less
If your output is wrapping a lot you can also utilize the -S
switch to less, which will force all the output to stay on a single line instead. You can then use your arrow keys to move left/right/up/down to see all of it.
$ ps aux | sort -rn -k 5,6 | less -S
Sorting within ps
Certain versions of ps
provide the ability to use --sort
. This switch can then take keys that are either prefixed with a +
or a -
to denote the sort order...least to greatest or greatest to least.
Examples
vsz,-rss
$ ps aux --sort=vsz,-rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 07:03 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:03 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 07:03 0:01 [migration/0]
root 5 0.0 0.0 0 0 ? S 07:03 0:00 [watchdog/0]
+vsz,+rss
$ ps aux --sort=+vsz,+rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 07:03 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:03 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 07:03 0:01 [migration/0]
root 5 0.0 0.0 0 0 ? S 07:03 0:00 [watchdog/0]
-vsz,-rss
$ ps aux --sort=-vsz,-rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1832 0.0 0.0 2088924 3312 ? Sl 07:04 0:00 /usr/sbin/console-kit-daemon --no-daemon
saml 3517 0.2 1.2 2073196 100492 ? Sl 07:06 0:34 /home/saml/.dropbox-dist/dropbox
saml 3516 0.0 0.8 2071032 67388 ? Sl 07:06 0:07 /home/saml/.dropbox-dist/dropbox
saml 2657 0.1 0.7 1580936 57788 ? Sl 07:05 0:27 nautilus
A quick and dirty method is to just pipe the output of ps aux
to the sort
command:
$ ps aux | sort -rn -k 5,6
Example
$ ps aux | sort -rn -k 5,6
...
root 1584 0.0 0.0 22540 1236 ? S 07:04 0:01 hald-addon-storage: polling /dev/sr0 (every 2 sec)
root 1575 0.0 0.0 22536 872 ? S 07:04 0:00 /usr/libexec/hald-addon-generic-backlight
root 1574 0.0 0.0 22536 880 ? S 07:04 0:00 /usr/libexec/hald-addon-leds
root 1565 0.0 0.0 22536 876 ? S 07:04 0:00 /usr/libexec/hald-addon-rfkill-killswitch
saml 2507 0.0 0.0 22232 500 ? S 07:05 0:00 dbus-launch --sh-syntax --exit-with-session
root 1671 0.0 0.0 22156 936 ? Ss 07:04 0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
...
This doesn't handle for the column headers which get mixed in with the output, but it's easy to remember on the command line, and is an acceptable way to do what you want when manually viewing this type of output.
Example
root 1791 0.0 0.0 4140 536 tty2 Ss+ 07:04 0:00 /sbin/mingetty /dev/tty2
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 996 0.0 0.0 0 0 ? S 07:04 0:01 [kdmflush]
root 982 0.0 0.0 0 0 ? S 07:04 0:00 [kvm-irqfd-clean]
More tips
An additional tip would be to pipe the entire output to another command such as less
. This allows you to look at the information a page at a time and also use the arrow keys and page up/down keys to scroll back and forth through the output.
$ ps aux | sort -rn -k 5,6 | less
If your output is wrapping a lot you can also utilize the -S
switch to less, which will force all the output to stay on a single line instead. You can then use your arrow keys to move left/right/up/down to see all of it.
$ ps aux | sort -rn -k 5,6 | less -S
Sorting within ps
Certain versions of ps
provide the ability to use --sort
. This switch can then take keys that are either prefixed with a +
or a -
to denote the sort order...least to greatest or greatest to least.
Examples
vsz,-rss
$ ps aux --sort=vsz,-rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 07:03 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:03 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 07:03 0:01 [migration/0]
root 5 0.0 0.0 0 0 ? S 07:03 0:00 [watchdog/0]
+vsz,+rss
$ ps aux --sort=+vsz,+rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 07:03 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:03 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 07:03 0:01 [migration/0]
root 5 0.0 0.0 0 0 ? S 07:03 0:00 [watchdog/0]
-vsz,-rss
$ ps aux --sort=-vsz,-rss | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1832 0.0 0.0 2088924 3312 ? Sl 07:04 0:00 /usr/sbin/console-kit-daemon --no-daemon
saml 3517 0.2 1.2 2073196 100492 ? Sl 07:06 0:34 /home/saml/.dropbox-dist/dropbox
saml 3516 0.0 0.8 2071032 67388 ? Sl 07:06 0:07 /home/saml/.dropbox-dist/dropbox
saml 2657 0.1 0.7 1580936 57788 ? Sl 07:05 0:27 nautilus
edited Nov 18 '16 at 3:10
answered Sep 26 '13 at 15:00
slm♦
246k66507673
246k66507673
willps
always output the columns in the way you expectsort
to see/process them?
– Felipe Alvarez
Aug 13 '15 at 2:34
Depends on which version of ps
– slm♦
Aug 13 '15 at 4:04
2
A... | less
is a good advice but sometimes your process has a huge command line and it clutters the output. In such cases... | less -S
works better.
– waste
Nov 17 '16 at 23:54
@waste - good tip, just remember that-S
truncates and so you may lose some of what you want to see, but otherwise good advice if you're only interested the left most columns.
– slm♦
Nov 18 '16 at 2:54
@slm I am not sure that is the case forless -S
. When you closeless
view everything disappears, but as long as you are in the view, you can scroll vertically but also horizontally. Copying might be difficult, though.
– waste
Nov 18 '16 at 3:03
|
show 2 more comments
willps
always output the columns in the way you expectsort
to see/process them?
– Felipe Alvarez
Aug 13 '15 at 2:34
Depends on which version of ps
– slm♦
Aug 13 '15 at 4:04
2
A... | less
is a good advice but sometimes your process has a huge command line and it clutters the output. In such cases... | less -S
works better.
– waste
Nov 17 '16 at 23:54
@waste - good tip, just remember that-S
truncates and so you may lose some of what you want to see, but otherwise good advice if you're only interested the left most columns.
– slm♦
Nov 18 '16 at 2:54
@slm I am not sure that is the case forless -S
. When you closeless
view everything disappears, but as long as you are in the view, you can scroll vertically but also horizontally. Copying might be difficult, though.
– waste
Nov 18 '16 at 3:03
will
ps
always output the columns in the way you expect sort
to see/process them?– Felipe Alvarez
Aug 13 '15 at 2:34
will
ps
always output the columns in the way you expect sort
to see/process them?– Felipe Alvarez
Aug 13 '15 at 2:34
Depends on which version of ps
– slm♦
Aug 13 '15 at 4:04
Depends on which version of ps
– slm♦
Aug 13 '15 at 4:04
2
2
A
... | less
is a good advice but sometimes your process has a huge command line and it clutters the output. In such cases ... | less -S
works better.– waste
Nov 17 '16 at 23:54
A
... | less
is a good advice but sometimes your process has a huge command line and it clutters the output. In such cases ... | less -S
works better.– waste
Nov 17 '16 at 23:54
@waste - good tip, just remember that
-S
truncates and so you may lose some of what you want to see, but otherwise good advice if you're only interested the left most columns.– slm♦
Nov 18 '16 at 2:54
@waste - good tip, just remember that
-S
truncates and so you may lose some of what you want to see, but otherwise good advice if you're only interested the left most columns.– slm♦
Nov 18 '16 at 2:54
@slm I am not sure that is the case for
less -S
. When you close less
view everything disappears, but as long as you are in the view, you can scroll vertically but also horizontally. Copying might be difficult, though.– waste
Nov 18 '16 at 3:03
@slm I am not sure that is the case for
less -S
. When you close less
view everything disappears, but as long as you are in the view, you can scroll vertically but also horizontally. Copying might be difficult, though.– waste
Nov 18 '16 at 3:03
|
show 2 more comments
Even if ps do not reflect the actual memory used, this command is pretty helpful.
ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }'
add a comment |
Even if ps do not reflect the actual memory used, this command is pretty helpful.
ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }'
add a comment |
Even if ps do not reflect the actual memory used, this command is pretty helpful.
ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }'
Even if ps do not reflect the actual memory used, this command is pretty helpful.
ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }'
answered Aug 3 '17 at 10:03
Pierozi
411
411
add a comment |
add a comment |
simple way is to install htop
in that you can sort process based on PID,Percentage CPU,MEM
more sophisticated
add a comment |
simple way is to install htop
in that you can sort process based on PID,Percentage CPU,MEM
more sophisticated
add a comment |
simple way is to install htop
in that you can sort process based on PID,Percentage CPU,MEM
more sophisticated
simple way is to install htop
in that you can sort process based on PID,Percentage CPU,MEM
more sophisticated
answered Aug 7 '17 at 12:55
Ayyanar
815
815
add a comment |
add a comment |
As an alternative to the BSD style arguments shown in the other answers, one can use (at least using procps, shipped by Debian and Ubuntu):
ps -eF --sort=-rss
add a comment |
As an alternative to the BSD style arguments shown in the other answers, one can use (at least using procps, shipped by Debian and Ubuntu):
ps -eF --sort=-rss
add a comment |
As an alternative to the BSD style arguments shown in the other answers, one can use (at least using procps, shipped by Debian and Ubuntu):
ps -eF --sort=-rss
As an alternative to the BSD style arguments shown in the other answers, one can use (at least using procps, shipped by Debian and Ubuntu):
ps -eF --sort=-rss
answered Oct 13 '16 at 23:27
user30747
1112
1112
add a comment |
add a comment |
- Run
top
command
Shift + F
to sort based on field (see the full menu below)- Select
n
to sort based on memory usage
n: %MEM = Memory usage (RES)
add a comment |
- Run
top
command
Shift + F
to sort based on field (see the full menu below)- Select
n
to sort based on memory usage
n: %MEM = Memory usage (RES)
add a comment |
- Run
top
command
Shift + F
to sort based on field (see the full menu below)- Select
n
to sort based on memory usage
n: %MEM = Memory usage (RES)
- Run
top
command
Shift + F
to sort based on field (see the full menu below)- Select
n
to sort based on memory usage
n: %MEM = Memory usage (RES)
answered Mar 1 at 11:51
Sabrina
28517
28517
add a comment |
add a comment |
ps aux --sort -rss is nice:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 5984 0.8 7.4 1632488 296056 ? Sl 06:30 6:18 /usr/lib/chromium-browser/chromium-browser --type=ren
user 23934 21.7 6.0 1565600 241228 ? Sl 15:45 40:10 /opt/atom/atom --type=renderer --enable-experimental-
user 5533 0.9 5.1 3154096 206376 ? SLl 06:30 6:47 /usr/lib/chromium-browser/chromium-browser --enable-p
user 17306 1.7 4.9 1360648 196124 ? Sl 18:14 0:36 /usr/lib/chromium-browser/chromium-browser --type=ren
user 22272 30.1 4.6 1347784 185032 ? Sl 18:43 1:54 /usr/lib/chromium-browser/chromium-browser --type=ren
user 19318 0.6 3.3 1304324 133452 ? Sl 18:27 0:09 /usr/lib/chromium-browser/chromium-browser --type=ren
user 22098 1.0 3.3 1298500 133216 ? Sl 18:43 0:04 /usr/lib/chromium-browser/chromium-browser --type=ren
but if you want to see memory and cpu usages by application (grouped by commands):
python3.6 sum_process_resources.py
==== CPU% ====
0. /opt/atom/atom | 27.8
1. /usr/lib/chromium-browser/chromium-browser | 11.2
2. python3.6 | 11.0
3. /opt/google/chrome/chrome | 1.6
4. /usr/lib/xorg/Xorg | 1.4
5. /opt/Franz/franz | 0.7
==== MEM% ====
0. /usr/lib/chromium-browser/chromium-browser | 37.2
1. /opt/google/chrome/chrome | 11.3
2. /opt/Franz/franz | 10.6
3. /opt/atom/atom | 10.1
4. /usr/lib/xorg/Xorg | 2.0
5. com.google.android.gms.persistent | 1.4
==== RSS MB ====
0. /usr/lib/chromium-browser/chromium-browser | 1475.07 MB
1. /opt/google/chrome/chrome | 461.35 MB
2. /opt/Franz/franz | 429.04 MB
3. /opt/atom/atom | 402.18 MB
4. /usr/lib/xorg/Xorg | 78.53 MB
5. com.google.android.gms.persistent | 58.02 MB
code:
#sum_process_resources.py
from collections import OrderedDict
import subprocess
def run_cmd(cmd_string):
"""Runs commands and saves output to variable"""
cmd_list = cmd_string.split(" ")
popen_obj = subprocess.Popen(cmd_list, stdout=subprocess.PIPE)
output = popen_obj.stdout.read()
output = output.decode("utf8")
return output
def sum_process_resources():
"""Sums top X cpu and memory usages grouped by processes"""
ps_memory, ps_cpu, ps_rss = {}, {}, {}
top = 6
output = run_cmd('ps aux').split("n")
for i, line in enumerate(output):
cleaned_list = " ".join(line.split())
line_list = cleaned_list.split(" ")
if i > 0 and len(line_list) > 10:
cpu = float(line_list[2])
memory = float(line_list[3])
rss = float(line_list[5])
command = line_list[10]
ps_cpu[command] = round(ps_cpu.get(command, 0) + cpu, 2)
ps_memory[command] = round(ps_memory.get(command, 0) + memory, 2)
ps_rss[command] = round(ps_rss.get(command, 0) + rss, 2)
sorted_cpu = OrderedDict(sorted(ps_cpu.items(), key=lambda x: x[1], reverse=True))
sorted_memory = OrderedDict(sorted(ps_memory.items(), key=lambda x: x[1], reverse=True))
sorted_rss = OrderedDict(sorted(ps_rss.items(), key=lambda x: x[1], reverse=True))
print("==== CPU% ====")
for i, k in enumerate(sorted_cpu.items()):
if i < top:
print("{}. {} | {}".format(i, k[0], k[1]))
print("==== MEM% ====")
for i, k in enumerate(sorted_memory.items()):
if i < top:
print("{}. {} | {}".format(i, k[0], k[1]))
print("==== RSS MB ====")
for i, k in enumerate(sorted_rss.items()):
if i < top:
print("{}. {} | {} MB".format(i, k[0], round((k[1]/1024), 2)))
if __name__ == '__main__':
sum_process_resources()
add a comment |
ps aux --sort -rss is nice:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 5984 0.8 7.4 1632488 296056 ? Sl 06:30 6:18 /usr/lib/chromium-browser/chromium-browser --type=ren
user 23934 21.7 6.0 1565600 241228 ? Sl 15:45 40:10 /opt/atom/atom --type=renderer --enable-experimental-
user 5533 0.9 5.1 3154096 206376 ? SLl 06:30 6:47 /usr/lib/chromium-browser/chromium-browser --enable-p
user 17306 1.7 4.9 1360648 196124 ? Sl 18:14 0:36 /usr/lib/chromium-browser/chromium-browser --type=ren
user 22272 30.1 4.6 1347784 185032 ? Sl 18:43 1:54 /usr/lib/chromium-browser/chromium-browser --type=ren
user 19318 0.6 3.3 1304324 133452 ? Sl 18:27 0:09 /usr/lib/chromium-browser/chromium-browser --type=ren
user 22098 1.0 3.3 1298500 133216 ? Sl 18:43 0:04 /usr/lib/chromium-browser/chromium-browser --type=ren
but if you want to see memory and cpu usages by application (grouped by commands):
python3.6 sum_process_resources.py
==== CPU% ====
0. /opt/atom/atom | 27.8
1. /usr/lib/chromium-browser/chromium-browser | 11.2
2. python3.6 | 11.0
3. /opt/google/chrome/chrome | 1.6
4. /usr/lib/xorg/Xorg | 1.4
5. /opt/Franz/franz | 0.7
==== MEM% ====
0. /usr/lib/chromium-browser/chromium-browser | 37.2
1. /opt/google/chrome/chrome | 11.3
2. /opt/Franz/franz | 10.6
3. /opt/atom/atom | 10.1
4. /usr/lib/xorg/Xorg | 2.0
5. com.google.android.gms.persistent | 1.4
==== RSS MB ====
0. /usr/lib/chromium-browser/chromium-browser | 1475.07 MB
1. /opt/google/chrome/chrome | 461.35 MB
2. /opt/Franz/franz | 429.04 MB
3. /opt/atom/atom | 402.18 MB
4. /usr/lib/xorg/Xorg | 78.53 MB
5. com.google.android.gms.persistent | 58.02 MB
code:
#sum_process_resources.py
from collections import OrderedDict
import subprocess
def run_cmd(cmd_string):
"""Runs commands and saves output to variable"""
cmd_list = cmd_string.split(" ")
popen_obj = subprocess.Popen(cmd_list, stdout=subprocess.PIPE)
output = popen_obj.stdout.read()
output = output.decode("utf8")
return output
def sum_process_resources():
"""Sums top X cpu and memory usages grouped by processes"""
ps_memory, ps_cpu, ps_rss = {}, {}, {}
top = 6
output = run_cmd('ps aux').split("n")
for i, line in enumerate(output):
cleaned_list = " ".join(line.split())
line_list = cleaned_list.split(" ")
if i > 0 and len(line_list) > 10:
cpu = float(line_list[2])
memory = float(line_list[3])
rss = float(line_list[5])
command = line_list[10]
ps_cpu[command] = round(ps_cpu.get(command, 0) + cpu, 2)
ps_memory[command] = round(ps_memory.get(command, 0) + memory, 2)
ps_rss[command] = round(ps_rss.get(command, 0) + rss, 2)
sorted_cpu = OrderedDict(sorted(ps_cpu.items(), key=lambda x: x[1], reverse=True))
sorted_memory = OrderedDict(sorted(ps_memory.items(), key=lambda x: x[1], reverse=True))
sorted_rss = OrderedDict(sorted(ps_rss.items(), key=lambda x: x[1], reverse=True))
print("==== CPU% ====")
for i, k in enumerate(sorted_cpu.items()):
if i < top:
print("{}. {} | {}".format(i, k[0], k[1]))
print("==== MEM% ====")
for i, k in enumerate(sorted_memory.items()):
if i < top:
print("{}. {} | {}".format(i, k[0], k[1]))
print("==== RSS MB ====")
for i, k in enumerate(sorted_rss.items()):
if i < top:
print("{}. {} | {} MB".format(i, k[0], round((k[1]/1024), 2)))
if __name__ == '__main__':
sum_process_resources()
add a comment |
ps aux --sort -rss is nice:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 5984 0.8 7.4 1632488 296056 ? Sl 06:30 6:18 /usr/lib/chromium-browser/chromium-browser --type=ren
user 23934 21.7 6.0 1565600 241228 ? Sl 15:45 40:10 /opt/atom/atom --type=renderer --enable-experimental-
user 5533 0.9 5.1 3154096 206376 ? SLl 06:30 6:47 /usr/lib/chromium-browser/chromium-browser --enable-p
user 17306 1.7 4.9 1360648 196124 ? Sl 18:14 0:36 /usr/lib/chromium-browser/chromium-browser --type=ren
user 22272 30.1 4.6 1347784 185032 ? Sl 18:43 1:54 /usr/lib/chromium-browser/chromium-browser --type=ren
user 19318 0.6 3.3 1304324 133452 ? Sl 18:27 0:09 /usr/lib/chromium-browser/chromium-browser --type=ren
user 22098 1.0 3.3 1298500 133216 ? Sl 18:43 0:04 /usr/lib/chromium-browser/chromium-browser --type=ren
but if you want to see memory and cpu usages by application (grouped by commands):
python3.6 sum_process_resources.py
==== CPU% ====
0. /opt/atom/atom | 27.8
1. /usr/lib/chromium-browser/chromium-browser | 11.2
2. python3.6 | 11.0
3. /opt/google/chrome/chrome | 1.6
4. /usr/lib/xorg/Xorg | 1.4
5. /opt/Franz/franz | 0.7
==== MEM% ====
0. /usr/lib/chromium-browser/chromium-browser | 37.2
1. /opt/google/chrome/chrome | 11.3
2. /opt/Franz/franz | 10.6
3. /opt/atom/atom | 10.1
4. /usr/lib/xorg/Xorg | 2.0
5. com.google.android.gms.persistent | 1.4
==== RSS MB ====
0. /usr/lib/chromium-browser/chromium-browser | 1475.07 MB
1. /opt/google/chrome/chrome | 461.35 MB
2. /opt/Franz/franz | 429.04 MB
3. /opt/atom/atom | 402.18 MB
4. /usr/lib/xorg/Xorg | 78.53 MB
5. com.google.android.gms.persistent | 58.02 MB
code:
#sum_process_resources.py
from collections import OrderedDict
import subprocess
def run_cmd(cmd_string):
"""Runs commands and saves output to variable"""
cmd_list = cmd_string.split(" ")
popen_obj = subprocess.Popen(cmd_list, stdout=subprocess.PIPE)
output = popen_obj.stdout.read()
output = output.decode("utf8")
return output
def sum_process_resources():
"""Sums top X cpu and memory usages grouped by processes"""
ps_memory, ps_cpu, ps_rss = {}, {}, {}
top = 6
output = run_cmd('ps aux').split("n")
for i, line in enumerate(output):
cleaned_list = " ".join(line.split())
line_list = cleaned_list.split(" ")
if i > 0 and len(line_list) > 10:
cpu = float(line_list[2])
memory = float(line_list[3])
rss = float(line_list[5])
command = line_list[10]
ps_cpu[command] = round(ps_cpu.get(command, 0) + cpu, 2)
ps_memory[command] = round(ps_memory.get(command, 0) + memory, 2)
ps_rss[command] = round(ps_rss.get(command, 0) + rss, 2)
sorted_cpu = OrderedDict(sorted(ps_cpu.items(), key=lambda x: x[1], reverse=True))
sorted_memory = OrderedDict(sorted(ps_memory.items(), key=lambda x: x[1], reverse=True))
sorted_rss = OrderedDict(sorted(ps_rss.items(), key=lambda x: x[1], reverse=True))
print("==== CPU% ====")
for i, k in enumerate(sorted_cpu.items()):
if i < top:
print("{}. {} | {}".format(i, k[0], k[1]))
print("==== MEM% ====")
for i, k in enumerate(sorted_memory.items()):
if i < top:
print("{}. {} | {}".format(i, k[0], k[1]))
print("==== RSS MB ====")
for i, k in enumerate(sorted_rss.items()):
if i < top:
print("{}. {} | {} MB".format(i, k[0], round((k[1]/1024), 2)))
if __name__ == '__main__':
sum_process_resources()
ps aux --sort -rss is nice:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 5984 0.8 7.4 1632488 296056 ? Sl 06:30 6:18 /usr/lib/chromium-browser/chromium-browser --type=ren
user 23934 21.7 6.0 1565600 241228 ? Sl 15:45 40:10 /opt/atom/atom --type=renderer --enable-experimental-
user 5533 0.9 5.1 3154096 206376 ? SLl 06:30 6:47 /usr/lib/chromium-browser/chromium-browser --enable-p
user 17306 1.7 4.9 1360648 196124 ? Sl 18:14 0:36 /usr/lib/chromium-browser/chromium-browser --type=ren
user 22272 30.1 4.6 1347784 185032 ? Sl 18:43 1:54 /usr/lib/chromium-browser/chromium-browser --type=ren
user 19318 0.6 3.3 1304324 133452 ? Sl 18:27 0:09 /usr/lib/chromium-browser/chromium-browser --type=ren
user 22098 1.0 3.3 1298500 133216 ? Sl 18:43 0:04 /usr/lib/chromium-browser/chromium-browser --type=ren
but if you want to see memory and cpu usages by application (grouped by commands):
python3.6 sum_process_resources.py
==== CPU% ====
0. /opt/atom/atom | 27.8
1. /usr/lib/chromium-browser/chromium-browser | 11.2
2. python3.6 | 11.0
3. /opt/google/chrome/chrome | 1.6
4. /usr/lib/xorg/Xorg | 1.4
5. /opt/Franz/franz | 0.7
==== MEM% ====
0. /usr/lib/chromium-browser/chromium-browser | 37.2
1. /opt/google/chrome/chrome | 11.3
2. /opt/Franz/franz | 10.6
3. /opt/atom/atom | 10.1
4. /usr/lib/xorg/Xorg | 2.0
5. com.google.android.gms.persistent | 1.4
==== RSS MB ====
0. /usr/lib/chromium-browser/chromium-browser | 1475.07 MB
1. /opt/google/chrome/chrome | 461.35 MB
2. /opt/Franz/franz | 429.04 MB
3. /opt/atom/atom | 402.18 MB
4. /usr/lib/xorg/Xorg | 78.53 MB
5. com.google.android.gms.persistent | 58.02 MB
code:
#sum_process_resources.py
from collections import OrderedDict
import subprocess
def run_cmd(cmd_string):
"""Runs commands and saves output to variable"""
cmd_list = cmd_string.split(" ")
popen_obj = subprocess.Popen(cmd_list, stdout=subprocess.PIPE)
output = popen_obj.stdout.read()
output = output.decode("utf8")
return output
def sum_process_resources():
"""Sums top X cpu and memory usages grouped by processes"""
ps_memory, ps_cpu, ps_rss = {}, {}, {}
top = 6
output = run_cmd('ps aux').split("n")
for i, line in enumerate(output):
cleaned_list = " ".join(line.split())
line_list = cleaned_list.split(" ")
if i > 0 and len(line_list) > 10:
cpu = float(line_list[2])
memory = float(line_list[3])
rss = float(line_list[5])
command = line_list[10]
ps_cpu[command] = round(ps_cpu.get(command, 0) + cpu, 2)
ps_memory[command] = round(ps_memory.get(command, 0) + memory, 2)
ps_rss[command] = round(ps_rss.get(command, 0) + rss, 2)
sorted_cpu = OrderedDict(sorted(ps_cpu.items(), key=lambda x: x[1], reverse=True))
sorted_memory = OrderedDict(sorted(ps_memory.items(), key=lambda x: x[1], reverse=True))
sorted_rss = OrderedDict(sorted(ps_rss.items(), key=lambda x: x[1], reverse=True))
print("==== CPU% ====")
for i, k in enumerate(sorted_cpu.items()):
if i < top:
print("{}. {} | {}".format(i, k[0], k[1]))
print("==== MEM% ====")
for i, k in enumerate(sorted_memory.items()):
if i < top:
print("{}. {} | {}".format(i, k[0], k[1]))
print("==== RSS MB ====")
for i, k in enumerate(sorted_rss.items()):
if i < top:
print("{}. {} | {} MB".format(i, k[0], round((k[1]/1024), 2)))
if __name__ == '__main__':
sum_process_resources()
answered 31 mins ago
Jozsef Turi
111
111
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f92493%2fsorting-down-processes-by-memory-usage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What would be the expected output ? check
ps
– Rahul Patil
Sep 26 '13 at 14:40
Additional examples of how to use
--sort
are here: alvinalexander.com/linux/…– slm♦
Sep 26 '13 at 15:16
1
Superset question, using any tool: stackoverflow.com/questions/4802481/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
May 21 '14 at 16:12