Python_ cleaning up memory-consuming scripts

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.