리눅스 프로세스 메모리 사용량 확인 - linugseu peuloseseu memoli sayonglyang hwag-in

'{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' |    cut -d "" -f2 | cut -d "-" -f1 | grep $1 | grep -v grep`;
free -m | grep -v Swap;
echo "";
sleep $2; done




실행 가능하도록 파일의 퍼미션을 변경합니다. 


$ chmod +x check_memory.sh




감시하려는 프로세스의 이름과 얼마의 간격으로 메모리를 출력할지를 초 단위로 입력한 것을 인자로 사용하여 스크립트를 실행합니다. 

여기에서는 감시하려는 프로세스 이름을 main으로 하고 1초 간격으로 메모리 상태를 출력하도록 합니다.


다음처럼 지정한 main 프로세스 메모리와 전체 시스템의 메모리 상태를 1초 간격으로 출력해줍니다. 


$ ./check_memory.sh main 1

20/07/05 13:21:49 1506.91 Mb ./main

              total        used        free      shared  buff/cache   available

Mem:           4944        1569         833          61        2541        2990


20/07/05 13:21:50 1506.91 Mb ./main

              total        used        free      shared  buff/cache   available

Mem:           4944        1570         832          61        2541        2989


20/07/05 13:21:51 1506.91 Mb ./main

              total        used        free      shared  buff/cache   available

Mem:           4944        1570         833          61        2541        2989


20/07/05 13:21:52 1506.91 Mb ./main

              total        used        free      shared  buff/cache   available

Mem:           4944        1570         832          61        2541        2989


^C  Ctrl + C를 눌러 종료시 까지 실행됩니다. 





다음처럼 스크립트 실행시  | tee output.log를 추가하면 다음처럼 화면에도 메모리 사용량을 보여주고 


$ ./check_memory.sh main 1| tee output.log

20/07/05 13:27:09 1506.91 Mb ./main

              total        used        free      shared  buff/cache   available

Mem:           4944        1568         833          61        2541        2990


20/07/05 13:27:10 1506.91 Mb ./main

              total        used        free      shared  buff/cache   available

Mem:           4944        1569         833          61        2541        2990


^C




동시에 지정한 파일 output.log에도 메모리 사용량이 저장됩니다. 


$ cat output.log 

20/07/05 13:27:09 1506.91 Mb ./main

              total        used        free      shared  buff/cache   available

Mem:           4944        1568         833          61        2541        2990


20/07/05 13:27:10 1506.91 Mb ./main

              total        used        free      shared  buff/cache   available

Mem:           4944        1569         833          61        2541        2990



참고

https://stackoverflow.com/questions/131303/how-to-measure-actual-memory-usage-of-an-application-or-process