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 the power file appropriately to point to your custom script.

Troubleshooting:

  1. After restarting acpid, check that acpid is actually running by using the pidof acpid command.

  2. Does your script work as expected? Use full path names for commands in your script. Do not assume any value for PATH.

  3. 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.

  4. 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 line HandlePowerKey=ignore, so that systemd-logind.service will ignore the power button.