programing tip

Linux에서 단일 프로세스의 CPU 사용량 및 메모리 사용량을 검색 하시겠습니까?

itbloger 2020. 6. 7. 10:36
반응형

Linux에서 단일 프로세스의 CPU 사용량 및 메모리 사용량을 검색 하시겠습니까?


Linux에서 단일 프로세스의 CPU 및 메모리 사용을 원합니다. PID를 알고 있습니다. 다행히도 'watch'명령을 사용하여 1 초마다 가져 와서 CSV에 쓸 수 있기를 바랍니다. Linux 명령 행에서이 정보를 얻는 데 어떤 명령을 사용할 수 있습니까?


ps -p <pid> -o %cpu,%mem,cmd

"cmd"를 사용하지 않아도되지만 디버깅에 도움이 될 수 있습니다.

이것은 프로세스가 실행되고있는 동안 프로세스의 평균 CPU 사용량을 나타냅니다.


카페 답변 의 변형 :top -p <pid>

이렇게하면 CPU 사용량이 자동으로 새로 고쳐 지므로 모니터링에 좋습니다.


프로세스 이름을 사용하여 결과를 얻을 수 있습니다.

ps -C chrome -o %cpu,%mem,cmd

-C옵션을 사용하면 프로세스 이름을 pid를 몰라도 사용할 수 있습니다.


pidstat를 사용하십시오 (sysstat에서- 링크 참조 ).

예를 들어 5 초마다이 두 프로세스 ID (12345 및 11223)를 모니터링하려면

$ pidstat -h -r -u -v -p 12345,11223 5

프로그램을 시작하고 모니터링

이 양식은 실행 파일을 쉽게 벤치마킹하려는 경우에 유용합니다.

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

이제 Ctrl + C를 누르면 프로그램이 종료되고 모니터링이 중지됩니다. 샘플 출력 :

CPU  MEM
20.0  1.3
35.0  1.3
40.0  1.3

관련 : https://unix.stackexchange.com/questions/554/how-to-monitor-cpu-memory-usage-of-a-single-process

우분투 16.04에서 테스트되었습니다.


에 주석으로 카페의 대답 PS, 위의 당신에게 PCPU의 수명 평균을 줄 것이다 pidstat 경우에. 보다 정확한 결과를 얻으려면 top을 사용하십시오. 한 번 실행해야하는 경우 다음을 실행할 수 있습니다.

top -b -n 1 -p <PID>

또는 데이터와 헤더 만 처리하는 경우 :

top -b -n 1 -p <PID> | tail -3 | head -2

헤더가없는 경우 :

top -b -n 1 -p <PID> | tail -2 | head -1

top -b원하는 pid를 사용 하고 grep ( -b플래그 상단이 배치 모드로 실행 됨)하거나 -pgrep을 사용하지 않고 플래그를 사용하고 pid를 지정할 수도 있습니다 .


ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr

또는 프로세스 당

ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr |grep mysql

ps 명령 (사용하지 않아야 함) :

top 명령 (사용해야 함) :

top실시간으로 CPU 사용량을 얻는 데 사용 합니다 (현재 짧은 간격).

top -b -n 2 -d 0.2 -p 6962 | tail -1 | awk '{print $9}'

다음과 같이 반향됩니다. 78.6


ps aux|awk  '{print $2,$3,$4}'|grep PID

where the first column is the PID,second column CPU usage ,third column memory usage.


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.


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 ...


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

For those who struggled for a while wonderring why the selected answer does not work:

ps -p <pid> -o %cpu,%mem

No SPACE ibetween %cpu, and %mem.


This is a nice trick to follow one or more programs in real time while also watching some other tool's output: watch "top -bn1 -p$(pidof foo),$(pidof bar); tool"


(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)


Above list out the top cpu and memory consuming process

        ps axo %cpu,%mem,command | sort -nr | head

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

참고URL : https://stackoverflow.com/questions/1221555/retrieve-cpu-usage-and-memory-usage-of-a-single-process-on-linux

반응형