经常我们会发现服务器跑着跑着内存使用率达到了百分之八九十,或者有时候直接挂掉,在我们还没定位是哪块代码有问题导致内存占用很大的时候,可以先写个定时脚本,当服务器内存使用率达到一定值的时候,就重启一起服务,释放内存。下面这个定时脚本是每隔10s去判断一下机器的内存,假如内存使用率超过10%,就重启一下进程(正常情况下内存使用率是%5左右,所以就定了个10%),代码如下面蓝色部分所示。然后修改脚本权限,用于命令让它在后头不挂断运行:nohup python -u restart.py >nohup.out 2>&1 &

#! /usr/bin/env python
# -*- coding:utf-8 -*-

import os
import time
import datetime
import sched
import datetime
import psutil

#schedule_time = sched.scheduler(time.time,time.sleep)

schedule = sched.scheduler(time.time,time.sleep)

#def perform_command_time(cmd,inc):
#   schedule_time.enter(inc,0,perform_command_time,(cmd,inc))
#   os.system(cmd)

def perform_command(cmd,inc):
    schedule.enter(inc,0,perform_command,(cmd,inc))
    memory = psutil.virtual_memory()
    percent = memory.percent
    if percent < 10:
        now = datetime.datetime.now()
        print now
        print percent
    if percent >=10:
        now = datetime.datetime.now()
        print now
        print percent
        os.system('ps -ef | grep supervisord | grep -v grep | cut -c 9-15 | xargs kill -9')
        os.system('ps -ef | grep ncq.dnsquery_monitor0_check | grep -v grep | cut -c 9-15 | xargs kill -9')
        time.sleep(5)
        os.system('supervisord -c /opt/trunk/conf.d/supervisord.conf')
        os.system('supervisorctl -uxxx -pxxx restart all')
        time.sleep(5)
#def timming_exe(cmd,inc=60):
#    schedule_time.enter(inc,0,perform_command_time,(cmd,inc))
#    schedule_time.run()

def restart_exe(cmd,inc=5):
    schedule.enter(inc,0,perform_command,(cmd,inc))
    schedule.run()

#print("show date after 10 seconds:")
#timming_exe('date',10)
print("restart celery----------->")
restart_exe('supervisorctl -uxxx -pxxx restart all',10)
#restart_exe('df -h',12)

python定时脚本判断服务器内存的更多相关文章

  1. Linux shell脚本判断服务器网络是否可以上网

    Linux shell脚本判断网络畅通 介绍 在编写shell脚本时,有的功能需要确保服务器网络是可以上网才可以往下执行,那么此时就需要有个函数来判断服务器网络状态 我们可以通过curl来访问 www ...

  2. 用apscheduler写python定时脚本

    apscheduler 官方文档:http://apscheduler.readthedocs.io/en/latest/ 写一个后台定时任务,一般2个选择,一个是apscheduler,一个cele ...

  3. 阿里云ECS服务器上搭建keepalived+mha+mysql5.6+gtid+一主两从+脚本判断架构踩的坑

    最近,公司项目搭建了一套后端数据库架构,不是在RDS,是在阿里云的ECS服务器上搭建keepalived.mha.mysql5.6.gtid.一主两从架构,目前还没有实现读写分离,以后架构升级,可能代 ...

  4. linux服务器内存、根目录使用率、某进程的监控告警脚本

    脚本内容如下 #!/bin/bash #磁盘超过百分之80发送邮件告警 DISK_USED=`df -T |sed -n "2p" |awk '{print ($4/$3)*100 ...

  5. [Python]python CGI脚本在apache服务器上运行时出现“Premature end of script headers”错误

    在测试自己的python CGI脚本时, 当html网页中的表单form内容传送到服务器python脚本时, 总是出现Premature end of script headers错误, 网页显示是服 ...

  6. 一步一步开发Game服务器(三)加载脚本和服务器热更新(二)完整版

    上一篇文章我介绍了如果动态加载dll文件来更新程序 一步一步开发Game服务器(三)加载脚本和服务器热更新 可是在使用过程中,也许有很多会发现,动态加载dll其实不方便,应为需要预先编译代码为dll文 ...

  7. 关于定时脚本crontab的坑

    需求: 每分钟执行一次程序,将处理后的数据写入mongodb 最初做法: 1):写crontab没有响应,于是打算通过shell脚本的while true来执行 当时sb,没控制时间内,而且我还是用n ...

  8. Windows server利用批处理脚本判断端口, 启动tomcat

    win server服务器上面的tomcat老是不定时挂掉, 于是利用定时操作脚本判断tomcat80端口是否在运行, 如果运行则放过, 如果down掉就启动tomcat,解决tomcat不定时挂掉导 ...

  9. Python定时框架 Apscheduler 详解【转】

    内容来自网络: https://www.cnblogs.com/luxiaojun/p/6567132.html 在平常的工作中几乎有一半的功能模块都需要定时任务来推动,例如项目中有一个定时统计程序, ...

随机推荐

  1. Qt中隐藏滚动条重新实现鼠标滚轮事件wheelEvent

    delta()已经被弃用了,QT5中用的是angleDelta(),计算的时候取angleDelta().y()值. #重载方法wheelEvent(self,event),即滚轮事件方法 #---- ...

  2. (23)socket多进程并发

    # 对于服务器自己本身,一个程序只能绑定一个端口 # 同一个端口可以多个客户端来连接, # 只要server_ip+ server_port +client_ip + cilent_port 不一样, ...

  3. Jmeter转换成中文模式

    本片文章转至:https://blog.csdn.net/him2014/article/details/79603887 下载安装好Jmeter后默认的是英文,对于我这种学渣来说简直就是受到了100 ...

  4. [Web Service] Tutorial Basic Concepts

    WSDL是网络服务描述语言,是一个包含关于web service信息(如方法名,方法参数)以及如何访问它. WSDL是UDDI的一部分. 作为web service 应用程序之间的接口,发音为wiz- ...

  5. NetSec2019 20165327 Exp6 信息搜集与漏洞扫描

    NetSec2019 20165327 Exp6 信息搜集与漏洞扫描 一.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 二.实践内容 1.各种搜索技巧的应用 2.DNS IP注册信息的查询 ...

  6. js用解构来定义变量并赋值

    解构数组 var [a,b]=[1,2]; a //1 b //2 ------------- var [a,b]=[1,2,3,4]; a //1 b //2 ---------------- va ...

  7. libcrypto.so.1.0.0: no version information available

    openssl-1.0.1p源码安装后,依赖于openssl.so库的应用报错libcrypto.so.1.0.0: no version information available 解法:1. 创建 ...

  8. Vue route的使用

    1.route.js文件 import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) const router = ne ...

  9. 将本地文件上传到GitHub

    首先,可参见廖雪峰老师的官方网站进行Git安装:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c ...

  10. python3.7导入gevent模块报错的解决方案

    最近更新了python解释器3.7 结果安装gevent,在导入gevent之后就报错了,错误信息如下 RuntimeWarning: greenlet.greenlet size changed, ...