如何检查交流笔记本电脑适配器是否插入?

use*_*682 12 bash scripts adapter

无论是否插入交流适配器,我都需要检查笔记本电脑启动时运行的 bash 脚本。这可能吗?

Rad*_*anu 14

您可以使用acpiwith-a参数。要查看它是如何工作的,请在您的终端中运行:

acpi -a
Run Code Online (Sandbox Code Playgroud)

默认情况下,acpi软件包未安装在 Ubuntu 中,但使用以下命令从终端安装非常简单快捷:

sudo apt-get install acpi
Run Code Online (Sandbox Code Playgroud)

然后,在您的脚本中,您可以使用例如:

ac_adapter=$(acpi -a | cut -d' ' -f3 | cut -d- -f1)

if [ "$ac_adapter" = "on" ]; then
    notify-send "AC Adapter" "The AC Adapter is on."
else
    notify-send "AC Adapter" "The AC Adapter is off."
fi
Run Code Online (Sandbox Code Playgroud)

要使脚本在启动时运行,只需在您的 crontab 列表中添加一个新条目(使用crontab -e命令),如下所示:

@reboot DISPLAY=:0.0 /path/to/your/script
Run Code Online (Sandbox Code Playgroud)