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
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
-b: Batch-mode-n 2: Number-of-iterations, use 2 because: When you first run it, it has no previous
sample to compare to, so these initial values are the percentages since boot.-d 0.2: Delay-time(in second, here is 200ms)-p 6962: Monitor-PIDstail -1: the last rowawk '{print $9}': the 9-th column(the cpu usage number)