MacOS Battery Tracker is an automated tool designed to monitor and analyze the health of your MacBook's battery over time. By tracking key metrics such as cycle count and maximum capacity, this application provides valuable insights into your battery's performance and degradation patterns.
- Automated Data Collection: Regularly captures battery cycle count and maximum capacity.
- Data Persistence: Stores collected data in a CSV file for long-term tracking.
- Visual Analytics: Generates time-series plots to visualize battery health trends.
- Effortless Monitoring: Eliminates the need for manual checks through system reports.
- Organized Data Storage:
- CSV data is stored in the
data
directory. - Generated plots are saved as PNG files in the
graphs
directory.
- CSV data is stored in the
Below are sample visualizations generated by the MacOS Battery Tracker:
-
Clone the repository:
git clone https://github.com/CedricDeVijt/MacOS_Battery_Tracker.git cd MacOS_Battery_Tracker
-
Set execution permissions for the script:
chmod +x battery_tracker.sh
Run the script manually:
./battery_tracker.sh
After running the script, you can find:
- The CSV file with battery data in the
data
directory. - The generated plots in the
graphs
directory.
-
Make the automation script executable:
chmod +x automate.sh
-
Run the automation script:
./automate.sh
This will set up a cron job to run the battery tracker every Monday at 12:00 PM.
-
Open your crontab file:
crontab -e
-
Add the following line (adjust the path as necessary):
0 12 * * 1 /path/to/battery_tracker.sh
-
Save and exit the crontab editor.
You can modify the cron job schedule by editing the automate.sh
file or by manually adjusting the crontab entry. Refer
to crontab.guru for assistance with cron job syntax.
- Python 3
- pandas
- matplotlib
Dependencies are automatically installed in a virtual environment when running the battery_tracker.sh
script.
If you encounter issues with cron job execution, check the system logs:
grep CRON /var/log/syslog
The MacOS Battery Tracker consists of several key components:
-
battery_tracker.sh: This shell script serves as the entry point for the application. It sets up a Python virtual environment, installs dependencies, and runs the main Python script.
-
battery_tracker.py: This is the core Python script that performs the following tasks:
- Retrieves battery data using system commands
- Updates the CSV file with new data
- Generates plots for visualization
-
automate.sh: This script automates the process of setting up a cron job for regular execution of the battery tracker.
The program uses the system_profiler
command-line tool to retrieve battery information:
cmd = ["/usr/sbin/system_profiler", "SPPowerDataType"]
result = subprocess.run(cmd, capture_output=True, text=True)
It then parses the output to extract the cycle count and maximum capacity.
Battery data is stored in a CSV file (data/battery_health_tracker.csv
) with the following structure:
Date,Cycle Count,Maximum Capacity (%)
The script checks if the cycle count has changed before adding a new entry, ensuring that data is only recorded when there's a meaningful change.
The program uses matplotlib to create two types of plots:
- Battery Capacity Over Time
- Battery Cycle Count Over Time
These plots are saved as PNG files in the graphs
directory.
Contributions to the MacOS Battery Tracker are welcome! Please feel free to submit pull requests or create issues for bugs and feature requests.
This project is licensed under the MIT License. See the LICENSE file for details.