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 在平常的工作中几乎有一半的功能模块都需要定时任务来推动,例如项目中有一个定时统计程序, ...
随机推荐
- CHM文件命令转换成html
在Windows下chm转换为html的超简单方法(反编译CHM文件的方法) 通过调用Windows命令,将chm 文件转换为html 文件. 方法: 命令行(cmd),输入hh -decompile ...
- og协议-有利于SNS网站分享
一丶前言 全球快递toWhere官网发现使用og协议,并且支付宝和淘宝活动源码也会添加og协议,查阅资料弄清og协议是什么,此刻附上og协议官方文档 一丶什么是og协议 Open Graph通讯协定( ...
- IDEA(添加类注释以及方法注释)
添加类注释: File---Setting----Editor----Code Style-----File and Code Templates--------Class #if (${PA ...
- 每日算法---Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- CRM创建BP(END USER)
FUNCTION ZCRM_BP_CRT. *"---------------------------------------------------------------------- ...
- CentOS 7上安装PGI 2017编译器
1. 安装PGI编译器 在PGI的官方网站的右上角,有一个社区免费版(Community Edition)的下载链接(GET PGI FOR FREE),根据操作系统选择合适的版本即可. 需要注意的是 ...
- css--样式表的引入方法
html引用css方法如下1.直接在div中使用css样式(内链)2.html中使用style自带式(嵌入)3.使用@import引用外部CSS文件(外部引入)4.使用 link引用外部CSS文件 推 ...
- java前的部分了解(计算机小白)
一.加密 对称加密: des 3des AES rc4 (数据加密) 会话密钥 非对称加密(成对:公钥/私钥(一个加密一个解密)):RSA DSA 密钥交换 / 数字签名(用私钥加密摘要算法出的一串数 ...
- RK3288 GPIO
简介GPIO, 全称 General-Purpose Input/Output(通用输入输出),是一种软件运行期间能够动态配置和控制的通用引脚.RK3288有9组 GPIO bank: GPIO0,G ...
- MVP之高级MVP架构封装
MVP之高级MVP架构封装 No MVP: 我们一般会这样写: public class MainActivity extends AppCompatActivity { private EditTe ...