Coder Perfect

On Linux, how do I get the CPU and memory consumption of a single process?


On Linux, I’d want to retrieve the CPU and memory use of a single process whose PID I know. Using the ‘watch’ command, I should be able to get it every second and save it to a CSV. What command can I use from the Linux command line to retrieve this information?

Asked by Supertux

Solution #1

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

(You can omit “cmd,” although it may be useful for debugging.)

It’s worth noting that this shows the process’s average CPU utilization over the time it’s been running.

Answered by caf

Solution #2

top -p pid> is a variation of caf’s response.

This automatically updates the CPU use, making it useful for monitoring.

Answered by Manki

Solution #3

(Do not use) the ps command:

top command (should use):

To get real-time CPU use (current short interval), use top:

awk ‘print $9’ | top -b -n 2 -d 0.2 -p 6962 | tail -1 | top -b -n 2 -d 0.2 -p 6962

will sound like this: 78.6

Answered by vikyd

Solution #4

You can get the results by searching for the process name.

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

You can use process name without knowing its pid if you use the -C option.

Answered by amit

Solution #5

Use the pidstat command (from sysstat – Refer Link).

For example, if you want to monitor these two process IDs (12345 and 11223) every 5 seconds, you can do

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

Answered by Neon

Post is based on