Laptops allow you to work wherever you want. Well, as long as there’s battery life in your laptop. Here’s how to check the battery on the Linux command line.
Disconnected from a mains AC adapter, your laptop is entirely dependent on its battery for everything. Turning on the screen, using the hard drives, accessing Wi-Fi, and reading user input all come to an abrupt halt if the battery isn’t up to the task.
Manufacturers can’t agree on whether leaving a laptop plugged in all the time is a good thing or a bad thing. If you don’t want to do that, then your laptop usually won’t have a 100% battery charge when you go out with it.
Batteries also deteriorate during their useful life. Therefore, an older battery cannot hold the same charge as it did when new. And it’s highly unlikely that even when it was brand new, you could get what the manufacturer advertised.
Knowing to keep an eye on battery power is an integral part of using a laptop, of course. Thats nothing new. But what if you need to check the battery from the command line?
Maybe you’re out and about remotely connecting to a laptop at home using SSH, and you can’t remember if it’s plugged in or running on battery power. Perhaps you use a laptop as a server with no GUI or with a tiled window manager and don’t have an on-screen display of battery charge.
Being able to find out your laptop’s power status on the command line means you can script those techniques as well.
Battery check with upower
upower The command can be used to find out what power and battery sources are available for your laptop. Once you have discovered them, you can request more details.
-e The (list) option lists all the power sources it can find.
The first entry is for the AC adapter. The second is the battery. You may have multiple batteries in your laptop. Also, keep in mind that the first battery is sometimes numbered one and sometimes numbered zero, depending on the manufacturer’s preferences.
The “DisplayDevice” input is not a power supply. It is a composite device that represents the status icon to display in desktop environments.
To take a closer look at our battery, we’ll use the
-i (information) and pass the full battery descriptor.
upower -i /org/freedesktop/UPower/devices/battery_BAT1
The two items of most interest are the “Time to Empty” value and the “Percentage” value. These give an indication of how long the battery can continue to power the laptop and the percentage of charge remaining in the battery.
An important point to note is that the duration is related to the current activity of the laptop. If the load on the laptop increases, that duration will decrease.
Using our test laptop remotely via an SSH connection meant that the laptop’s built-in screen wasn’t being used. It was automatically deleted after a short period of time. With the screen blank, the laptop’s battery life was more than an hour longer than with the screen lit.
If the AC adapter is connected, the information returned by
upower it is slightly different.
upower -i /org/freedesktop/UPower/devices/battery_BAT1
The “Time to empty” value has been replaced by the “Time to fill” value, which is the time remaining before the battery reaches 100%. The “icon name” value has also changed to “symbolic of full battery charge”, reflecting the presence of mains power.
We can also take a deeper look at the AC adapter.
upower -i /org/freedesktop/UPower/devices/line_power_ACAD
The “Online” value will show “yes” if the AC adapter is plugged in and “no” if it is unplugged.
Examining the contents of /sys/class/power_supply/
On a laptop, the “/sys/class/power_supply/” directory contains information that we can take advantage of. Two subdirectories, “ACAD” and “BAT1”, contain information that we can reference to check battery capacity and whether the AC adapter is plugged in.
Note that the battery subdirectory might be named “BAT0” on your laptop. If you have multiple batteries installed on your laptop, you will have multiple battery subdirectories.
A file named “online” in the “ACAD” subdirectory contains the digit one if the AC adapter is plugged in and the digit zero if it is not.
A file called “capacity” in the subdirectory “BAT1” contains the value of the state of charge of the battery.
This laptop has the AC adapter plugged in and the battery charge is 81%.
Because these two values are presented in a straightforward and unadorned manner, they are ideal for use in scripting.
Let’s say you have a backup script that you only want to run if AC power is present or if the battery charge is greater than 70%. This script snippet shows how you might achieve this.
#!/bin/bash charge_level="$(cat /sys/class/power_supply/BAT1/capacity)" ac_adapter="$(cat /sys/class/power_supply/ACAD/online)" if [[ ac_adapter -eq 0 ]]; then if [[ charge_level < 70 ]]; then echo "Insufficient battery charge for backup:" $charge_level else echo "Sufficient battery charge, starting backup:" $charge_level fi else echo "On Mains power, starting backup." fi
The script gets the values from the two files and stores them in the variables
If the AC adapter is not plugged in, the value in
ac_adapter will be zero. If that is the case, the script checks the battery charge on
charge_level. If the battery charge exceeds 70%, the backup runs.
If the AC adapter is plugged in, the backup runs and the script doesn’t bother to check the battery charge value.
Copy the script into an editor and save it as “battery.sh”. Make sure you use the correct path to the battery subdirectory on your laptop.
We need to make the script executable with the
chmod +x battery.sh
Now we can run the script. AC adapter is plugged in.
Let’s unplug the AC adapter and run it again.
The power condition of the laptop is successfully detected and the script acts accordingly.
battery check with acpi
If you have the advanced settings and power interface package installed, you can use it to check the battery and power status of your notebook. If you don’t have it installed, it’s a small package and installs very quickly.
On Ubuntu, you can install it with this command.
sudo apt install acpi
In Fedora you will use:
sudo dnf install acpi
In Manjaro you should write:
sudo pacman -Sy acpi
We will use the command with the
-a (AC adapter) and then once again with the
-b (battery) option. Finally, we will execute it with the
-b (battery) and option
-i (information) option. This gives a bit of additional information if any is available.
knowledge is power
And now you can learn about powering your laptop.
The ability to have scripts check for mains power or enough battery power to perform long or high-load tasks like system images or updates is particularly powerful.
RELATED: How to Maximize Your Linux Laptop’s Battery Life