Vijeth Revankar ✅

Sep 30, 2024 • 2 min read

Pop!_OS Power Tricks: The Art of Automatic Profile Switching

A simple guide to automatic profile switching for optimized performance in Pop!_OS.

Pop!_OS Power Tricks: The Art of Automatic Profile Switching

Context :
I've got a 3.75 year old gaming laptop running Pop!_OS 22.04 (which is a fork of Ubuntu) by System76. Now, I used to be all about Windows, but I decided to take the plunge and go full Linux as my daily driver. It's been quite the ride!

I first gave Ubuntu 22.04 a shot, but man, it was missing some stuff I needed and had a few bugs that were driving me nuts. So, I hopped over to Pop!_OS and boom! Problems solved.

Don't get me wrong, I'm loving Pop!_OS, but there are still some Windows goodies I really miss. One of which was auto-switching power profiles on plugging/unplugging my machine. I didn't care much about it initially, but ever since I started using my laptop away from my desk, I felt something essential missing.

That's actually what got me thinking about writing this article.

Note: I've tried and tested this only on Pop!_OS 22.04 and not on other distributions. This guide/script uses system76-power which is a Power Profile Management tool built in with Pop!_OS. For other distributions, feel free to tweak the script accordingly.

To automatically switch between power modes on Pop!_OS 22.04 when plugging/unplugging your laptop, you can use the system76-power command line tool with an udev rule.

  1. Creating a script that will handle the power mode switching (and brightness)

    #!/bin/bash
    
    # Function to set brightness
    set_brightness() {
        # Get maximum brightness
        max_brightness=$(cat /sys/class/backlight/*/max_brightness)
        
        # Calculate new brightness
        new_brightness=$(echo "$max_brightness * $1" | bc -l | xargs printf "%.0f")
        
        # Set new brightness
        echo $new_brightness | sudo tee /sys/class/backlight/*/brightness
    }
    
    if [ "$1" = "on" ]; then
        system76-power profile performance
        set_brightness 1.0  # Set to 100% brightness
    elif [ "$1" = "off" ]; then
        system76-power profile battery
        set_brightness 0.6  # Set to 60% brightness
    fi
    

    If you're looking to only switch power modes and not brightness, use this script

    #!/bin/bash
    
    if [ "$1" = "on" ]; then
        system76-power profile performance
    elif [ "$1" = "off" ]; then
        system76-power profile battery
    fi
  2. Save this script as /usr/local/bin/power-mode-switch.sh and make it executable.

    $ sudo nano /usr/local/bin/power-mode-switch.sh
      # Paste the contents of the script in the text editor, 
      # here I'm using nano. You can use any editor of your choice
    
    $ sudo chmod +x /usr/local/bin/power-mode-switch.sh
      # Using this to make the file executable
  3. Create an udev rule to trigger this script when the power cable is plugged or unplugged.

    Save this rule as /etc/udev/rules.d/99-power-mode-switch.rules

    $ sudo nano /etc/udev/rules.d/99-power-mode-switch.rules
    ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", RUN+="/usr/local/bin/power-mode-switch.sh off"
    ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", RUN+="/usr/local/bin/power-mode-switch.sh on"
  4. Reload the udev rules.

    sudo udevadm control --reload-rules

That's all.

Now, when you unplug your laptop, it will automatically switch to Battery Life mode, and when you plug it in, it should switch to High Performance mode.

To check if the script is working, run

$ system76-power profile

Join Vijeth on Peerlist!

Join amazing folks like Vijeth and thousands of other people in tech.

Create Profile

Join with Vijeth’s personal invite link.

0

13

0