elma.dev - Can ELMA 10月02日
使用upower命令行工具管理Linux电源设备
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何使用upower命令行工具在GNU/Linux系统中枚举电源设备、监听设备事件以及查询历史和统计数据。通过使用该工具,用户可以获取有关无线鼠标、笔记本电脑电池等电源源的信息,并查看其电池电量。文章详细讲解了如何使用--dump和--show-info等命令行选项来获取设备信息,并提供了使用awk和grep命令提取特定信息的示例。此外,还介绍了一个简单的shell脚本,该脚本可以自动化获取电池百分比、容量、更新时间和空电时间等数据,方便用户进行电源管理。

🔌 upower是GNU/Linux系统中用于枚举电源设备、监听设备事件以及查询历史和统计数据的抽象工具,支持列出所有电源设备并获取详细信息。

📊 使用--dump (-d)选项可以列出所有电源设备及其路径,而--show-info (-i)选项则需要提供设备路径来获取特定设备的详细信息,如电池百分比、状态等。

🔍 通过结合awk和grep等命令,可以高效地从upower输出中提取所需信息,例如仅显示电池百分比或一次性列出所有电池的关键状态信息。

📈 文章提供了一个shell脚本示例,该脚本可以自动化获取电池的百分比、容量、更新时间和空电时间等数据,方便用户进行电源管理自动化或特定程序开发。

UPower is an abstraction for enumerating power devices, listening to device events and querying history and statistics. Using the upower command-line tool on GNU/Linux, you can get information about your power sources like the batteries your wireless mouse or laptop, and see their battery levels.

In this article, let's see how we can gather such information and use in a script.

How to use upower

First of all, list your power sources with the --dump, -d flag :

$ upower -d

If you have multiple power sources and want to show information about only one of them, you need to copy the device path starting with /org/freedesktop/UPower/devices of your target power source from the output of the previous command.

You need to pass that path using the --show-info, -i flag. For my laptop battery, I use the command below:

$ upower -i /org/freedesktop/UPower/devices/battery_BAT0

Or I could use the below command to only see the battery percentage:

$ upower -i /org/freedesktop/UPower/devices/battery_BAT0 | awk '/percentage/' {print $NF}'87%

With awk, we can extract the desired information like above. Here's one another example with the grep command to list all the information at once:

$ upower -i $(upower -e | grep 'BAT') | grep -E "state|to\ (full|empty)|percentage"

Note that the above command is only good when you want to print some data in the terminal. For an advanced alternative, keep reading.

Extract and show battery information in a script

You may want to use the battery percentage or similar information in a shell script to make some automations or specific programs. If you have such goal, you can find a simple script I wrote below:

battery
#!/bin/bash### Author: Can ELMA### Website: https://elma.dev### This script is intended to be used to show information about power sources.# YOU CAN EDIT THE LINES BELOWDEFAULT_DEVICE_NAME='battery_BAT0'# YOU CAN EDIT THE LINES ABOVE# ! DO NOT EDIT VARIABLES BELOW ! #DEVICE_NAME=${1:-'battery_BAT0'}PATH_PREFIX='/org/freedesktop/UPower/devices'DEVICE_PATH="$PATH_PREFIX/$DEVICE_NAME"# ! DO NOT EDIT VARIABLES ABOVE ! #if [ $# -gt 1 ]; then  echo "Only one parameter is allowed."  echo "Usage: '$0 <optional:device_name>'"  exit 1fi# GATHER INFORMATION ABOUT THE BATTERY DEVICEINFORMATION=$(upower -i "$DEVICE_PATH" 2> /dev/null)PERCENTAGE=$(awk '/percentage/ {print $NF}' <<< $INFORMATION)CAPACITY=$(awk '/capacity/ {print $NF}' <<< $INFORMATION)UPDATED=$(awk -F '(' '/updated/ {print $NF}' <<< $INFORMATION | sed -r 's/\)//')TIME_TO_EMPTY=$(awk -F ':' '/time to empty/ {print $NF}' <<< $INFORMATION | sed -r 's/\s{2,}//g')print_info() {  [ $# -ne 2 ] && echo "print_info() requires two parameters." && exit 1  printf "%s\t%s\n" "$1" "$2"}print_info "percentage:" "$PERCENTAGE"print_info 'capacity:' "$CAPACITY"print_info 'updated:' "$UPDATED"print_info 'time to empty:' "$TIME_TO_EMPTY"

You can change the default device name to the name of your power source by renaming the DEFAULT_DEVICE_NAME variable, or can pass that information to the script as an argument. Paste the codes above to a file named battery. With the chmod +x ./battery command, make it executable.

Usage:

$ ./batterypercentage:     37%capacity:       75.4833%updated:        44 seconds agotime to empty:  2.8 hours
or
$ ./battery battery_BAT0percentage:     92%capacity:       84.1224%updated:        46 seconds agotime to empty:  5.9 hours

That's all.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

upower Linux 电源管理 命令行工具 电池信息 shell脚本
相关文章