shell - Retrieve CPU usage and memory usage of a single process on Linux?

ID : 20362

viewed : 20

Tags : linuxshellmemory-managementcpu-usagelinux

Top 5 Answer for shell - Retrieve CPU usage and memory usage of a single process on Linux?

vote vote

96

ps -p <pid> -o %cpu,%mem,cmd 

(You can leave off "cmd" but that might be helpful in debugging).

Note that this gives average CPU usage of the process over the time it has been running.

vote vote

89

A variant of caf's answer: top -p <pid>

This auto-refreshes the CPU usage so it's good for monitoring.

vote vote

80

ps command (should not use):

top command (should use):

Use top to get CPU usage in real time(current short interval):

top -b -n 2 -d 0.2 -p 6962 | tail -1 | awk '{print $9}'

will echo like: 78.6

vote vote

67

You can get the results by the name of the process using

ps -C chrome -o %cpu,%mem,cmd 

the -C option allows you to use process name without knowing it's pid.

vote vote

51

Use pidstat (from sysstat - Refer Link).

e.g. to monitor these two process IDs (12345 and 11223) every 5 seconds use

$ pidstat -h -r -u -v -p 12345,11223 5 

Top 3 video Explaining shell - Retrieve CPU usage and memory usage of a single process on Linux?

Related QUESTION?