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
A variant of caf's answer:
top -p <pid>
This auto-refreshes the CPU usage so it's good for monitoring.
All of the answers here show only the memory percentage for the PID.
Here's an example of how to get the rss memory usage in KB for all apache processes, replace "grep apache" with "grep PID" if you just want to watch a specific PID:
watch -n5 "ps aux -y | grep apache | awk '{print \$2,\$6}'"
This prints:
Every 5.0s: ps aux -y | grep apache | awk '{print $2,$6}'
Thu Jan 25 15:44:13 2018
12588 9328
12589 8700
12590 9392
12591 9340
12592 8700
12811 15200
15453 9340
15693 3800
15694 2352
15695 1352
15697 948
22896 9360
With CPU %:
watch -n5 "ps aux -y | grep apache | awk '{print \$2,\$3,\$6}'"
Output:
Every 5.0s: ps aux -y | grep apache | awk '{print $2,$3,$6}'
Thu Jan 25 15:46:00 2018
12588 0.0 9328
12589 0.0 8700
12590 0.0 9392
12591 0.0 9340
12592 0.0 8700
12811 0.0 15200
15453 0.0 9340
15778 0.0 3800
15779 0.0 2352
15780 0.0 1348
15782 0.0 948
22896 0.0 9360
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.