python_清理占用内存的脚本

脚本内容如下:

vim    ~/shell/clean_cache.py

 1 #!/usr/bin/python
 2 #conding=utf-8
 3 #该脚本用于清理服务器当中占用内存20G以上,运行时间超过3小时以上(或1天以上的)进程。
 4 import os
 5 #import psutil
 6 
 7 pslist = os.popen('ps -auxf','r').readlines()
 8 for list in pslist:
 9         p = list.split()                           #以空格分割
10         user = p[0]                                #用户
11         pid = p[1]                                    #进程PID    
12         mem = p[4]                                    #内存
13         day = p[8]                                    #日期
14         t = p[9]                                    #运行时间
15         if mem.isdigit()  and int(mem) > 20000000:  #剔除第一行vsz并且保证占用内存大于20G
16                 if day.isalnum():                   #判断日期是否为今天
17                         print day.isalnum()
18                         print "progess running over a day,kill it now!"
19                         cmd = "kill -9 "+pid
20                         os.popen(cmd)
21                 if int(t.split(':')[0]) > 180:      #判断运行时间超过3小时
22                         print t.split(':')[0]
23                         print "this progesss have run over 2 hour,kill it now!"
24                         cmd = "kill -9 "+pid
25                         os.popen(cmd)
26         else:   
27                 continue 

以上是单台的服务器的脚本内容,其实可以结合crontab或者可以与ansible的script模块来批量执行,会达到更好的效果!

举例: ansible all -m script -s -a "/home/xxx/shell/clean_cache.sh"

 

转载于:https://www.cnblogs.com/to-be-rich/p/7580693.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值