views:

733

answers:

3

I want to get the CPU and memory usage of a single process on Linux - I know the PID. Hopefully, I can get it every second and write it to a CSV using the 'watch' command. What command can I use to get this info from the Linux command-line?

A: 

You could use top -b and grep out the pid you want (with the -b flag top runs in batch mode), or also use the -p flag and specify the pid without using grep.

Alberto Zaccagni
+3  A: 
ps -p <pid> -o %cpu,%mem,cmd

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

caf
This isn't very accurate. It includes the memory used by shared libraries.
Paul Biggar
The assumption would be that if you care about a single processes' memory usage enough to monitor it like this, it's using a significant amount of memory so that the extra couple-of-megabytes due to shared mappings isn't an issue.
caf
A: 

To get the memory usage of just your application (as opposed to the shared libraries it uses, you need to use the Linux smaps interface). This answer explains it well.

Paul Biggar