The script reads as follows:
Vim ~/shell/clean_cache.py
1 #!/usr/bin/python2 #Conding=utf-83 #This script is used to clean up the server that consumes more than 20G of memory and runs for more than 3 hours (or more than 1 days) of the process. 4 ImportOS5 #Import Psutil6 7PsList = Os.popen ('PS-AUXF','R'). ReadLines ()8 forListinchPsList:9p = list.split ()#Split with SpacesTenuser = P[0]#User OnePID = p[1]#Process PID AMEM = p[4]#Memory -Day = P[8]#Date -t = p[9]#Run Time the ifMem.isdigit () andInt (MEM) > 20000000:#Remove the first line of vsz and ensure that memory is greater than 20G - ifDay.isalnum ():#determine if the date is today - PrintDay.isalnum () - Print "progess running over a day,kill it now!" +cmd ="kill-9"+PID - os.popen (cmd) + ifInt (T.split (':') [0]) > 180:#judge running time over 3 hours A PrintT.split (':') [0] at Print "This progesss has run over 2 Hour,kill it now!" -cmd ="kill-9"+PID - os.popen (cmd) - Else: - Continue
The above is a single server script content, in fact, can be combined with crontab or Ansible script module to batch execution, will achieve better results!
Example: Ansible all-m script-s-a "/home/xxx/shell/clean_cache.sh"
Python_ cleaning up memory-consuming scripts