How to make sh script and run it when physical poweroff button is pressed? How to remove standard handler with user confirmation for this button?
Asked
Active
Viewed 765 times
1 Answers
1
To remove standard handler with user confirmation for poweroff button:
- Install
dconf
editor:sudo apt install -y dconf-cli dconf-editor
- Run
dconf
- Set option
/org/gnome/settings-daemon/plugins/power/power-button-action
tonothing
. - To check its value in console:
gsettings list-recursively org.gnome.settings-daemon.plugins.power | grep power-button-action
- Configuration for this setting will be stored in user home directory.
To set up physical poweroff button handler:
Create sh script:
sudo touch /etc/acpi/poweroff.sh
sudo chmod +x /etc/acpi/poweroff.sh
sudo vim /etc/acpi/poweroff.sh
- Put to it the following content:
#!/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
For example, the following commands can be used for corresponding actions:
sudo systemctl poweroff
sudo systemctl suspend
sudo systemctl hibernate
sudo systemctl reboot
Create poweroff physical button event handler:
sudo touch /etc/acpi/events/power
sudo vim /etc/acpi/events/power
- Put to it the following content:
event=button/power LNXPWRBN
action=/etc/acpi/poweroff.sh "%e"
Apply changes:
sudo /etc/init.d/acpid restart
When the button is pressed new line will be added to the log file
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
Related link: Disable shutdown confirmation for physical "Power off" button in the dash (Ubuntu 20.04)

Anton Samokat
- 805