This Python snippet monitors the size of a specified file and alerts the user if the file grows beyond a specified threshold. This is useful for logging systems, large data files, or any scenario where unexpected file growth could indicate an issue.
import os
import time
def monitor_file_size(file_path, max_size_mb, check_interval=60):
"""
Monitor a file's size and alert if it exceeds a threshold.
Args:
file_path (str): Path to the file to monitor.
max_size_mb (float): Maximum allowed file size in megabytes.
check_interval (int): Time between checks in seconds (default: 60).
"""
max_size_bytes = max_size_mb * 1024 * 1024 # Convert MB to bytes
try:
while True:
if not os.path.exists(file_path):
print(f"Error: File {file_path} does not exist.")
break
file_size = os.path.getsize(file_path)
file_size_mb = file_size / (1024 * 1024)
if file_size > max_size_bytes:
print(f"ALERT: File {file_path} has exceeded {max_size_mb} MB. Current size: {file_size_mb:.2f} MB")
else:
print(f"File size OK: {file_size_mb:.2f} MB / {max_size_mb} MB")
time.sleep(check_interval)
except KeyboardInterrupt:
print("\nMonitoring stopped.")
except Exception as e:
print(f"An error occurred: {e}")
# Example usage
if __name__ == "__main__":
file_to_monitor = "/var/log/syslog" # Example file (adjust as needed)
monitor_file_size(file_to_monitor, max_size_mb=10) # Alert if >10MB
This script:
max_size_mb) and check interval (check_interval) as needed.file_monitor.py.file_to_monitor and max_size_mb in the example usage.python3 file_monitor.py
Ctrl+C to stop monitoring.For production use, consider extending this script to send email alerts or integrate with logging systems.