python定时脚本判断服务器内存
经常我们会发现服务器跑着跑着内存使用率达到了百分之八九十,或者有时候直接挂掉,在我们还没定位是哪块代码有问题导致内存占用很大的时候,可以先写个定时脚本,当服务器内存使用率达到一定值的时候,就重启一起服务,释放内存。下面这个定时脚本是每隔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定时脚本判断服务器内存的更多相关文章
- Linux shell脚本判断服务器网络是否可以上网
Linux shell脚本判断网络畅通 介绍 在编写shell脚本时,有的功能需要确保服务器网络是可以上网才可以往下执行,那么此时就需要有个函数来判断服务器网络状态 我们可以通过curl来访问 www ...
- 用apscheduler写python定时脚本
apscheduler 官方文档:http://apscheduler.readthedocs.io/en/latest/ 写一个后台定时任务,一般2个选择,一个是apscheduler,一个cele ...
- 阿里云ECS服务器上搭建keepalived+mha+mysql5.6+gtid+一主两从+脚本判断架构踩的坑
最近,公司项目搭建了一套后端数据库架构,不是在RDS,是在阿里云的ECS服务器上搭建keepalived.mha.mysql5.6.gtid.一主两从架构,目前还没有实现读写分离,以后架构升级,可能代 ...
- linux服务器内存、根目录使用率、某进程的监控告警脚本
脚本内容如下 #!/bin/bash #磁盘超过百分之80发送邮件告警 DISK_USED=`df -T |sed -n "2p" |awk '{print ($4/$3)*100 ...
- [Python]python CGI脚本在apache服务器上运行时出现“Premature end of script headers”错误
在测试自己的python CGI脚本时, 当html网页中的表单form内容传送到服务器python脚本时, 总是出现Premature end of script headers错误, 网页显示是服 ...
- 一步一步开发Game服务器(三)加载脚本和服务器热更新(二)完整版
上一篇文章我介绍了如果动态加载dll文件来更新程序 一步一步开发Game服务器(三)加载脚本和服务器热更新 可是在使用过程中,也许有很多会发现,动态加载dll其实不方便,应为需要预先编译代码为dll文 ...
- 关于定时脚本crontab的坑
需求: 每分钟执行一次程序,将处理后的数据写入mongodb 最初做法: 1):写crontab没有响应,于是打算通过shell脚本的while true来执行 当时sb,没控制时间内,而且我还是用n ...
- Windows server利用批处理脚本判断端口, 启动tomcat
win server服务器上面的tomcat老是不定时挂掉, 于是利用定时操作脚本判断tomcat80端口是否在运行, 如果运行则放过, 如果down掉就启动tomcat,解决tomcat不定时挂掉导 ...
- Python定时框架 Apscheduler 详解【转】
内容来自网络: https://www.cnblogs.com/luxiaojun/p/6567132.html 在平常的工作中几乎有一半的功能模块都需要定时任务来推动,例如项目中有一个定时统计程序, ...
随机推荐
- Python字符串 --Python3
Python语言中,字符串是用两个双引号或者单引号括起来的词汇表或多个字符. 1.Python字符串的两种序号体系 反向递减序号:-- -9 -8 -7 -6 -5 -4 -3 -2 -1 正向递增序 ...
- Oracle中国移动经典面试题(附代码跟两种答案)
/*中国移动sql面试题: create table test( id number(10) primary key, type number(10) , t_id number(10), ...
- 【HDU - 1429】胜利大逃亡(续) (高级搜索)【状态压缩+BFS】
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开 ...
- pyqt多线程进度条
ui.py # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'ui.ui' # # Creat ...
- 理解不为人知的ClassLoader
JAVA类装载方式,有两种: 1.隐式装载, 程序在运行过程中当碰到通过new 等方式生成对象时,隐式调用类装载器加载对应的类到jvm中. 2.显式装载, 通过class.forname()等方法,显 ...
- 配置python3
1. 下载解压.$ wget https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tgz$ tar zxvf Python-3.4.1.tgz 2 ...
- 多个yml文件的读取方式
1配置pom.xml文件,以下配置将默认激活-dev.yml配置文件<profiles> <profile> <id>dev&l ...
- mysql:mysql is neither service nor target!?(suse12 sp2 )
今天想在自己的虚拟机上安装mysql,安装好了并且初始化之后,一直无法启动mysql. 1.service mysql start; 报错:mysql is neither service nor t ...
- Ubuntu18.04下安装搜狗输入法
Ubuntu18.04下安装搜狗输入法 第一步:安装 fcitx输入框架 sudo apt-get install fcitx 第二步:在官网下载 Linux 版本搜狗输入法 https://piny ...
- 建设工程造价数据服务云平台(计价BIM)
经过多次沟通和对招标文件的理解,招标人通过软件平台建立和使用人员库.项目库.材料设备价格库.数据库等四库的真实需求,本着招标人对“成果文件”(指造价.招标代理的计价成果),即“项目库”的计价文件,不打 ...