A Python script that continuously monitors and logs system resources and battery status.
- Monitors CPU, memory, and disk usage
- Tracks processes consuming high CPU and memory
- Logs battery status and estimated remaining time when on battery power
- Creates detailed logs with timestamps
- Python 3.6+
- psutil library
- Install the required dependency:
pip install psutil
- Make the script executable (optional):
chmod +x system_monitor.py
Run the script:
python3 system_monitor.py
The script will:
- Create a
logs
directory if it doesn't exist - Log information to both the console and
logs/system_monitor.log
- Run continuously until interrupted with Ctrl+C
You can modify these variables at the top of the script:
INTERVAL
: Time between checks (default: 60 seconds)CPU_THRESHOLD
: Minimum CPU percentage to report (default: 10%)MEMORY_THRESHOLD
: Minimum memory percentage to report (default: 5%)TOP_PROCESSES
: Number of top processes to log (default: 5)
2023-05-15 14:30:00 - INFO - System monitoring started
2023-05-15 14:30:00 - INFO - System: Darwin 21.6.0
2023-05-15 14:30:00 - INFO - Monitoring interval: 60 seconds
2023-05-15 14:30:00 - INFO - CPU threshold: 10.0%, Memory threshold: 5.0%
2023-05-15 14:30:00 - INFO - --------------------------------------------------------------------------------
2023-05-15 14:30:01 - INFO - System Load - CPU: 15.2%, Memory: 65.3% (10.45 GB/16.00 GB), Disk: 75.1% (350.25 GB/500.00 GB)
2023-05-15 14:30:01 - INFO - Battery Status: Power: Battery (45%), Remaining: 2h 30m
2023-05-15 14:30:01 - INFO - Top CPU-intensive processes:
2023-05-15 14:30:01 - INFO - PID: 1234, Name: chrome, User: user, CPU: 25.3%
2023-05-15 14:30:01 - INFO - PID: 5678, Name: firefox, User: user, CPU: 15.7%
2023-05-15 14:30:01 - INFO - Top memory-intensive processes:
2023-05-15 14:30:01 - INFO - PID: 1234, Name: chrome, User: user, Memory: 12.5%
2023-05-15 14:30:01 - INFO - --------------------------------------------------------------------------------
To run the script in the background:
nohup python3 system_monitor.py > /dev/null 2>&1 &
To stop the background process:
pkill -f system_monitor.py