python3实现磁盘空间监控
作者:djstavaV 时间:2023-07-09 14:12:49
本文实例为大家分享了python3磁盘空间监控的具体代码,供大家参考,具体内容如下
软硬件环境
python3
apscheduler
前言
在做频繁操作磁盘的python项目时,经常会碰到磁盘空间不足的情况,这个时候,工程应该要有自己的处理模块,当磁盘利用率到达某个点时,发出警告并停止程序的运行。本文就利用Python3中的apscheduler模块来处理这样的问题。
代码实践
import os
import sys
import signal
import logging
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
# 开启磁盘空间检测
sched = BackgroundScheduler()
# 间隔5分钟开启一个检查
intervalTrigger = IntervalTrigger(minutes=5)
# 给检查任务设个id,方便任务的取消
sched.add_job(spaceMonitorJob, trigger=intervalTrigger, id='id_space_monitor')
sched.start()
# 禁止apscheduler相关信息屏幕输出
logging.getLogger('apscheduler.executors.default').propagate = False
方法spaceMonitorJob代码如下
def spaceMonitorJob():
'''
当磁盘(切片存储的目录)利用率超过90%,程序退出
:return:
'''
try:
st = os.statvfs('/')
total = st.f_blocks * st.f_frsize
used = (st.f_blocks - st.f_bfree) * st.f_frsize
except FileNotFoundError:
print('check webroot space error.')
logger.error('check webroot space error.')
# 移除任务,病关闭sched任务
sched.remove_job(job_id='id_space_monitor')
sched.shutdown(wait=False)
sys.exit(-3)
if used / total > 0.9:
print('No enough space.')
logger.debug('No enough space.')
sched.remove_job(job_id='id_space_monitor')
sched.shutdown(wait=False)
# 杀掉进程
os.killpg(os.getpgid(os.getpid()), signal.SIGKILL)
# 退出
exit(-3)
来源:https://blog.csdn.net/djstavaV/article/details/78283891
标签:python,磁盘空间
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL+HTML+PHP 一个简单论坛网站的综合开发案例(注册、登录、注销、修改信息、留言等)
2023-06-14 15:54:17
![](https://img.aspxhome.com/file/2023/5/55525_0s.png)
运行tensorflow python程序,限制对GPU和CPU的占用操作
2022-07-03 02:49:26
如何让python的运行速度得到提升
2023-04-26 21:48:35
oracle执行cmd的实现方法
2009-04-24 12:10:00
一个很棒的js图片代码
2009-12-17 18:36:00
![](https://img.aspxhome.com/file/UploadPic/200912/17/texiao-10s.gif)
python版单链表反转
2021-08-20 17:11:12
![](https://img.aspxhome.com/file/2023/4/77734_0s.jpg)
如何使用moment.js获取本周、前n周、后n周开始结束日期及动态计算周数
2024-05-28 15:40:15
Python数据结构队列解决约瑟夫斯问题
2022-01-07 03:20:53
mac下重置mysl8.0.11密码的方法
2024-01-28 06:27:50
Python内置数据结构与操作符的练习题集锦
2022-07-25 05:12:53
python pandas库的安装和创建
2023-09-25 22:49:02
![](https://img.aspxhome.com/file/2023/7/98827_0s.png)
navicat无法远程连接mysql的解决方法
2024-01-21 13:13:51
python+matplotlib绘制3D条形图实例代码
2022-05-03 16:36:56
![](https://img.aspxhome.com/file/2023/1/80721_0s.png)
启动targetcli时遇到错误解决办法
2023-03-20 05:59:43
js中使用使用原型(prototype)定义方法的好处详解
2024-04-22 13:03:54
修改 CentOS 6.x 上默认Python的方法
2023-08-07 10:02:59
安装完Python包然后找不到模块的解决步骤
2022-01-23 05:38:10
![](https://img.aspxhome.com/file/2023/9/127919_0s.jpg)
搞清楚 Python traceback的具体使用方法
2022-10-04 07:00:15
详解JS 比较两个Json对象的值是否相等的实例
2024-04-29 13:35:36
Mysql实现企业级日志管理、备份与恢复的实战教程
2024-01-13 16:30:16
![](https://img.aspxhome.com/file/2023/7/115267_0s.png)