如何制作 sh 脚本并在按下物理关机按钮时运行它?如何删除此按钮的标准处理程序并让用户确认?
答案1
要删除带有用户确认的关机按钮的标准处理程序:
- 安装
dconf
编辑器:sudo apt install -y dconf-cli dconf-editor
- 跑步
dconf
- 将选项设置
/org/gnome/settings-daemon/plugins/power/power-button-action
为nothing
。 - 要在控制台中检查其值:
gsettings list-recursively org.gnome.settings-daemon.plugins.power | grep power-button-action
- 此设置的配置将存储在用户主目录中。
要设置物理关机按钮处理程序:
创建sh脚本:
sudo touch /etc/acpi/poweroff.sh
sudo chmod +x /etc/acpi/poweroff.sh
sudo vim /etc/acpi/poweroff.sh
- 输入以下内容:
#!/bin/bash
LOG_FILE='/var/log/poweroff.log'
#touch $LOG_FILE && chmod 0666 $LOG_FILE
touch $LOG_FILE && chmod a=rw $LOG_FILE
echo "$(date '+%Y.%m.%d %H:%M:%S.%3N'): poweroff button pressed, event: $1" >> $LOG_FILE
sudo systemctl poweroff
例如可以使用以下命令进行相应的操作:
sudo systemctl poweroff
sudo systemctl suspend
sudo systemctl hibernate
sudo systemctl reboot
创建关机物理按钮事件处理程序:
sudo touch /etc/acpi/events/power
sudo vim /etc/acpi/events/power
- 输入以下内容:
event=button/power LNXPWRBN
action=/etc/acpi/poweroff.sh "%e"
应用更改:
sudo /etc/init.d/acpid restart
按下按钮时,新行将添加到日志文件
sudo less /var/log/poweroff.log
2022.02.24 02:14:55.322: poweroff button pressed, event: button/power PBTN 00000080 00000000
2022.02.24 02:14:55.327: poweroff button pressed, event: button/power LNXPWRBN:00 00000080 00000014