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?
相关问题
- How to get the return code of a shell script in lu
- What uses more memory in c++? An 2 ints or 2 funct
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Achieving the equivalent of a variable-length (loc
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.Above list out the top cpu and memory consuming process
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, use2
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)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.
or per process
CPU and memory usage of a single process on Linux or you can get the top 10 cpu utilized processes by using below command
ps -aux --sort -pcpu | head -n 11