Jeg*_*ggy 5 notification power-management
我的笔记本电脑电池很烂,在不充电的情况下可持续使用长达 5 分钟。有时充电器在我不知道的情况下掉下来,导致计算机意外关闭。
有没有办法在我的充电器掉下来时收到通知?不像 Ubuntu 那样需要为电池充电(因为这对我的笔记本电脑不起作用,我已经放弃了这个想法)
简单的线性命令:
[[ $(acpi -b | grep -o "Discharging") ]] && notify-send "Alert" "Battery is not charging.\n Please plug your AC adapter!"
Run Code Online (Sandbox Code Playgroud)
从man acpi
我们有:
NAME
acpi - Shows battery status and other ACPI information
SYNOPSIS
acpi [options]
DESCRIPTION
acpi Shows information from the /proc or the /sys filesystem, such as battery status or thermal information.
OPTIONS
-b | --battery
show battery information
Run Code Online (Sandbox Code Playgroud)
如果您跑步acpi -b
并且电池处于充电模式,您将得到以下结果:
Battery 0: Charging, 88%, 00:38:17 until charged
Run Code Online (Sandbox Code Playgroud)
如果您的电池没有充电,那么结果将是这样的:
Battery 0: Discharging, 87%, 03:46:06 remaining
Run Code Online (Sandbox Code Playgroud)
然后我们正在使用以下命令查看结果中的“放电”:
acpi -b | grep -o "Discharging"
Run Code Online (Sandbox Code Playgroud)
如果电池未在充电,结果将是“正在放电”。
最后,如果我们从上面的命令中得到“放电”,我们会向通知发送警报:
[[ $(acpi -b | grep -o "Discharging") ]] && notify-send "Alert" "Battery is not charging.\n Please plug your AC adapter!"
Run Code Online (Sandbox Code Playgroud)
注意:[[ Something ]]
始终为真,[[ ! Something ]]
始终为假。
现在我们在后台运行它是最简单的方法,在一个 while 循环中。然后我将命令放入 while 循环中,并检查 X 时间间隔之间的电池状态。像这样:
while true
do
# Our command
sleep [number of seconds] # check the status every [number of seconds] seconds
done
Run Code Online (Sandbox Code Playgroud)
如果你想在启动时每 5 分钟运行一次脚本,那么构造将是:
ChkCharge.sh
在我的主目录中命名)/etc/rc.local
以调用您的脚本(您的ChkCharge.sh
)+“&”以使其退出。喜欢bash /home/USERNAME/ChkCharge.sh &
。最终剧本
#!/bin/bash
while true
do
[[ $(acpi -b | grep -o "Discharging") ]] && notify-send "Alert" "Battery is not charging.\n Please plug your AC adapter!"
sleep 300 # 300 seconds or 5 minutes
done
Run Code Online (Sandbox Code Playgroud)
结束。重新启动,看看魔法;)