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?
问题:
回答1:
ps -p -o %cpu,%mem,cmd
(You can leave off "cmd" but that might be helpful in debugging).
Note that this gives average CPU usage of the process over the time it has been running.
回答2:
A variant of caf's answer: top -p
This auto-refreshes the CPU usage so it's good for monitoring.
回答3:
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.
回答4:
Use pidstat (from sysstat - Refer Link).
e.g. to monitor these two process IDs (12345 and 11223) every 5 seconds use
$ pidstat -h -r -u -v -p 12345,11223 5
回答5:
Launch a program and monitor it
This form is useful if you want to benchmark an executable easily:
topp() ( $* &>/dev/null & pid="$!" trap ':' INT echo 'CPU MEM' while sleep 1; do ps --no-headers -o '%cpu,%mem' -p "$pid"; done kill "$pid" ) topp ./myprog arg1 arg2
Now when you hit Ctrl + C it exits the program and stops monitoring. Sample output:
CPU MEM 20.0 1.3 35.0 1.3 40.0 1.3
Tested on Ubuntu 16.04.
回答6:
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.
回答7:
ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr
or per process
ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr |grep mysql
回答8:
As commented in caf's answer above, ps and in some cases pidstat will give you the lifetime average of the pCPU. To get more accurate results use top. If you need to run top once you can run:
top -b -n 1 -p
or for process only data and header:
top -b -n 1 -p | tail -3 | head -2
without headers:
top -b -n 1 -p | tail -2 | head -1
回答9:
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.
回答10:
ps aux|awk '{print $2,$3,$4}'|grep PID
where the first column is the PID,second column CPU usage ,third column memory usage.
回答11:
ps axo pid,etime,%cpu,%mem,cmd | grep 'processname' | grep -v grep
PID - Process ID
etime - Process Running/Live Duration
%cpu - CPU usage
%mem - Memory usage
cmd - Command
Replace processname with whatever process you want to track, mysql nginx php-fpm etc ...
回答12:
(If you are in MacOS 10.10, try the accumulative -c option of top:
top -c a -pid PID
(This option is not available in other linux, tried with Scientific Linux el6 and RHEL6)
回答13:
Above list out the top cpu and memory consuming process
ps axo %cpu,%mem,command | sort -nr | head
回答14:
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