Run script when power button pressed
Solution 1:
Your question is answered in the EXAMPLE section of man acpid
. I will adapt to your needs.
- As a
root
create a file named/etc/acpi/events/power
with contents like this:
event=button/power
action=/usr/bin/logger "ACPI_POWER_BTTN_TEST: %e"
-
Then run
service acpid restart
. -
Tail the
syslog
file like this/usr/bin/tail -f /var/log/syslog
-
Press the power button to see your test message in
syslog
. -
Change the
action=
line in thepower
file appropriately to point to your custom script.
Troubleshooting:
-
After restarting
acpid
, check thatacpid
is actually running by using thepidof acpid
command. -
Does your script work as expected? Use full path names for commands in your script. Do not assume any value for
PATH
. -
In the above example I used the the
/usr/bin/logger
command as an easy way to log something somewhere (/var/log/syslog
). Alternatively, you can write something to a file under/tmp
for testing purposes. -
It has been reported (by @nathan) that some window managers may also capture the power button. It may help ensuring that
/etc/systemd/logind.conf
has the lineHandlePowerKey=ignore
, so thatsystemd-logind.service
will ignore the power button.