mac-battery-calibrate-helper/discharge-and-shutdown.sh

115 lines
No EOL
3.2 KiB
Bash

#!/bin/bash
# Battery Calibration Script
# This script monitors battery level and shuts down when it reaches below 6%
# It keeps the system awake during the process
# Check if script is run with sudo
if [ "$EUID" -ne 0 ]; then
echo "This script must be run as root (sudo)."
exit 1
fi
# Function to get battery percentage
get_battery_percentage() {
local percentage
percentage=$(system_profiler SPPowerDataType | grep "State of Charge (%)" | awk '{print $5}')
echo "$percentage"
}
# Function to check if charging
is_charging() {
local charging
charging=$(system_profiler SPPowerDataType | grep -A 1 "Charging:" | tail -1 | awk '{print $2}')
if [ "$charging" == "Yes" ]; then
return 0 # true
else
return 1 # false
fi
}
# Function to keep system awake - using caffeinate
keep_awake() {
# Start caffeinate in background to prevent sleep
# -d prevents display sleep, -i prevents idle sleep
caffeinate -d -i &
echo $! > /tmp/caffeinate_pid
}
# Function to stop keeping system awake
stop_awake() {
if [ -f /tmp/caffeinate_pid ]; then
kill $(cat /tmp/caffeinate_pid) 2>/dev/null
rm /tmp/caffeinate_pid
fi
}
# Clean up on script exit
cleanup() {
stop_awake
echo "Script terminated. System may now sleep normally."
exit 0
}
# Set up trap for clean exit
trap cleanup SIGINT SIGTERM EXIT
# Print instructions
echo "====== Battery Calibration Tool ======"
echo "This tool will help you calibrate your battery by:"
echo "1. Monitoring battery level until it reaches ~5%"
echo "2. Shutting down the system automatically"
echo "3. After shutdown, charge to 100% and run this script again"
echo "4. Repeat 3 times for complete calibration"
echo ""
echo "Your system will be kept awake during this process."
echo "Press Ctrl+C to exit the script at any time."
echo "======================================="
# Start keeping system awake
keep_awake
echo "System will be kept awake until battery reaches ~5%."
# Initial variables
current_cycle=0
max_cycles=3
battery_percentage=$(get_battery_percentage)
echo "Starting battery percentage: $battery_percentage%"
echo "Monitoring battery level. This window must remain open."
# Main loop to monitor battery level
while true; do
battery_percentage=$(get_battery_percentage)
charging_status=$(is_charging && echo "Yes" || echo "No")
clear
echo "====== Battery Calibration Tool ======"
echo "Current battery level: $battery_percentage%"
echo "Charging: $charging_status"
echo "Target level for shutdown: <6%"
echo "Calibration cycle: $((current_cycle + 1))/$max_cycles"
echo "======================================="
# Check if battery is below threshold
if [ "$battery_percentage" -lt 6 ]; then
echo "Battery level below 6%. System will shut down in 1 minute."
echo "After reboot, charge to 100% before running this script again."
# Increment cycle count in a persistent file
current_cycle=$((current_cycle + 1))
echo "$current_cycle" > /tmp/battery_calibration_cycle
# Clean up and prepare for shutdown
stop_awake
# Shutdown with 1 minute delay
shutdown -h +1
echo "Shutdown scheduled. You can cancel with: sudo shutdown -c"
exit 0
fi
# Wait 60 seconds before checking again
sleep 60
done