Automatic shutdown of a computer when the battery is low

The script checks the battery level and automatically shuts down the computer if the charge is below the specified threshold.

#!/bin/bash

# Battery level threshold in percentage
THRESHOLD=15

# Getting the current battery level
BATTERY_LEVEL=$(acpi -b | grep -P -o '[0-9]+(?=%)')

# Checking the battery level and shutting down if below the threshold
if [ "$BATTERY_LEVEL" -lt "$THRESHOLD" ]; then
  echo "Low battery level: $BATTERY_LEVEL%. Shutting down..."
  shutdown -h now
fi

Comments (0)

Leave a comment