Lab

bPower

Script
Linux
Laptop
Battery
Power Management
systemd

A small Linux laptop power-mode switcher that automatically flips between battery-saving and performance mode based on AC state.

Published
April 27, 2026
Updated
April 27, 2026

Usage Notes

Practical commands, script details, and implementation notes.

A tiny Linux helper for laptops that live in a constant cycle of:

  • plugged in, compile everything
  • unplugged, please stop eating battery like an idiot

So I made bPower.

It is just a shell script plus a systemd timer that checks whether the charger is connected and flips the machine between two modes:

  • Battery mode when unplugged
  • Performance mode when AC is connected

Nothing fancy. Just useful.

Why I made it

When the charger is plugged in, I want the laptop to behave like a workstation.

When it is running on battery, I want it to stop acting like a tiny space heater with ambitions.

I did not want to keep switching governors, turbo, and brightness by hand, so this became a small repeatable fix instead of another thing to remember.

What it does

StateBehavior
Plugged inPerformance mode, turbo on, brighter screen
On batteryPower-saving mode, turbo off, lower brightness

Requirements

This version is written for Debian / Ubuntu / Mint-style systems:

sudo apt install powertop brightnessctl

If you are on something else, translate the package install step accordingly.

Script

Create the script:

sudo nano /usr/local/bin/bpower.sh

Adjust the brightness values to your own laptop before you start treating 80% and 15% like universal truth.

Paste this:

#!/bin/bash

STATE_FILE="/run/bpower.state"

# Detect AC status (any Mains device)
AC_STATUS="0"
for POWER_DEVICE in /sys/class/power_supply/*; do
    TYPE=$(cat "$POWER_DEVICE/type" 2>/dev/null)

    if [ "$TYPE" = "Mains" ]; then
        ONLINE=$(cat "$POWER_DEVICE/online" 2>/dev/null)

        if [ "$ONLINE" = "1" ]; then
            AC_STATUS="1"
        fi
    fi
done

# Read previous state
PREV_STATE=""
if [ -f "$STATE_FILE" ]; then
    PREV_STATE=$(cat "$STATE_FILE")
fi

# Map to readable state
if [ "$AC_STATUS" = "1" ]; then
    CURRENT_STATE="AC"
else
    CURRENT_STATE="BAT"
fi

# If nothing changed, do nothing
if [ "$CURRENT_STATE" = "$PREV_STATE" ]; then
    exit 0
fi

# Save new state
echo "$CURRENT_STATE" > "$STATE_FILE"

# Apply settings only on change
if [ "$CURRENT_STATE" = "AC" ]; then
    echo "Switching to PERFORMANCE mode..."

    echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor > /dev/null
    echo 0 | tee /sys/devices/system/cpu/intel_pstate/no_turbo > /dev/null

    brightnessctl set 80% > /dev/null 2>&1

    echo "PERFORMANCE mode ON"
else
    echo "Switching to BATTERY mode..."

    echo powersave | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor > /dev/null
    echo 1 | tee /sys/devices/system/cpu/intel_pstate/no_turbo > /dev/null

    powertop --auto-tune > /dev/null 2>&1
    brightnessctl set 15% > /dev/null 2>&1

    echo "BATTERY mode ON"
fi

Make it executable:

sudo chmod +x /usr/local/bin/bpower.sh

Test it manually

sudo /usr/local/bin/bpower.sh

Then plug or unplug the charger and run it again to verify that the state actually changes the way you expect.

systemd integration

Service

sudo nano /etc/systemd/system/bpower.service
[Unit]
Description=bPower automatic laptop power mode switcher

[Service]
Type=oneshot
ExecStart=/usr/local/bin/bpower.sh

Timer

sudo nano /etc/systemd/system/bpower.timer
[Unit]
Description=Run bPower every 30 seconds

[Timer]
OnBootSec=10
OnUnitActiveSec=30
Unit=bpower.service

[Install]
WantedBy=timers.target

Enable it:

sudo systemctl daemon-reload
sudo systemctl enable --now bpower.timer

Debug

Check the timer:

systemctl status bpower.timer

Check logs:

journalctl -u bpower.service -n 50

Notes

  • Works well on most Debian / Ubuntu / Mint-style setups.
  • Power source detection is dynamic, so there is no hardcoded AC device path.
  • The intel_pstate/no_turbo part is Intel-specific and may not exist on every machine.
  • If your system does not expose /sys/devices/system/cpu/intel_pstate/no_turbo, remove or adapt that line instead of pretending it will magically work.
  • Brightness values are personal. 80% and 15% just happen to be reasonable on my laptop, not on Earth in general.

Made for dev laptops that live somewhere between containers and battery anxiety.