769 word3

AWS EC2 wordpress memory leak python auto check script

%ED%95%9C%EB%8B%AC100%EB%8B%AC%EB%9F%AC word3

694 word3

The Python script I made this time was made for me to use.
If the AWS EC2 instance keeps stopping due to lack of memory due to WordPress while operating it!!
Please solve it using the Python automatic script that I made right now.

The script checks the memory capacity and automatically restarts the Apache server when the desired percentage is reached.

Suppose my server has 8GB of total memory.
The Apache will restart automatically when 6.4GB is reached with 80% setup.

You can copy and use the script below to the Linux server.

directions for use

324 word3

 

이번에 만든 파이썬 스크립트는 제가 사용하기 위해서 만들었습니다.

혹시라도 AWS EC2 인스턴스를 운영중에 워드프레스 때문에 메모리 부족현상으로 자꾸 멈춘다면!!

지금 바로 제가 만든 파이썬 자동 스크립트를 이용하셔서 해결해보시기 바랍니다.

 

이 스크립트는 메모리 용량을 체크해서 원하는 퍼센트 수치에 도달할경우 자동으로 아파치 서버를 재시작합니다.

내 서버 전체 메모리가 8GB 라고 가정한다면,

80% 설정을 통해 6.4GB에 도달할 경우 자동으로 아파치가 restart 합니다.

 

아래 스크립트를 리눅스 서버에 복사해서 사용하면됩니다.

import psutil
import os
import stdin
import datetime as dt
import sys

try:
   from _______init_memory_limit_value import limit
except ImportError:
   print("Check!! FileName: '_______init_memory_limit_value.py'")
   print("sys.exit(0)")
   os.system("echo \"limit = 97\" > _______init_memory_limit_value.py")
   sys.exit(0)

x = dt.datetime.now()
mem = psutil.virtual_memory().percent

print("MEMORY percent is %d%%" % mem)
print("MEMORY limit is over %d%%" % limit)


if mem > limit:
   directoryName = "/root/_PYTHON3/_logs"
   try:
      if os.path.exists(directoryName):
         print("Directory already exists")
      else:
         os.makedirs(directoryName)

   except OSError:
   print("Check!! Error: Failed to create a directory")

   os.system("echo \"restart\" > " + directoryName + "/httpd_restart_" + x.strftime("%Y-%m-%d_%H:%M:%S"))
   os.system("service httpd restart")
   os.system("echo \"RESTART\"")

os.system("free -m")

 

 

사용법

python3 /root/_PYTHON3/memcheck_httpd_restart.py
Do it three times.

2번 실행해야합니다.

 

1번째 실행하면

_______init_memory_limit_value.py 파일을 만들면서 limit 제한 기본값 97로 정해집니다.

2번째실행하면 변수값을 바탕으로 결과를 보여줍니다.

743 word3

 

limit 변수값을 각자 서버 운영에 필요한 수치로 바꾸시면됩니다.

 

이제 1분마다 크론탭 이용해서 반복 실행하면됩니다.

 

769 word3

만약 메모리 부족 누수로 인해서 아파치 서버가 재실행을 하게된다면

_logs 폴더가 자동으로 생성되면서 시간이 기록됩니다.

 

#크론탭 1분마다 실행 등록

crontab -e
*/1 * * * * python3 /root/_PYTHON3/memcheck_httpd_restart.py