4

When I push the power button, I want to run a script. For the sake of this scenario, say file.sh. So when I push the power button, I want to run file.sh and only file.sh.

Most of the solutions I found involved editing /etc/acpi/powerbtn.sh, but that file does not exist on my system. (I'm running Ubuntu 19.04) I tried creating it and populating it with the standard contents, but it didn't work. I also tried listening for key presses with acpi_listen, but nothing happened when I pushed keys, so I don't know if I just did it wrong or what. I tried using xev,which did pick up key events but nothing registered when I hit the power button.

Anyone know how to accomplish this, or what I am doing wrong?

P.S. I should mention, I would prefer a solution that can be done on the command line, ideally one without any 3rd party software. Thanks.


Similar question, but with no solutions:

https://askubuntu.com/questions/473693/run-a-script-when-power-button-is-pushed

Basically looking for the Linux equivalent of what they did here:

https://stackoverflow.com/questions/12434863/executing-a-batch-script-on-windows-shutdown


EDIT: this is what I get from syslog:

Feb 9 13:01:01 joe-Aspire-E5-576G gnome-session-binary[1624]: Entering running state

And this is my file in events:

event=button/power action=/home/ragnvaldr/Desktop/test/sc.sh `

2 Answers 2

4

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.

4
  • I have tried this solution previously but it does not work. I get no feedback in syslog when pressing the power button on my machine. Feb 9, 2020 at 17:59
  • I just edited the original post with the output from the terminal when I followed your solution. Feb 9, 2020 at 18:04
  • Ok, so I got feadback working, but it wont run my script, this is my file: event=button/power action=/home/jeremiahgage/Desktop/test/sc.sh Feb 9, 2020 at 18:58
  • (1) Check that acpid is running with pidof acpid. (2) Does your script logs something to a file? I had user logger command as an easy way to log something somewhere (syslog). But, you can write something to a file in /tmp for example...
    – FedKad
    Feb 10, 2020 at 9:20
-1

Basically looking for the Linux equivalent of what they did here: https://stackoverflow.com/questions/12434863/executing-a-batch-script-on-windows-shutdown

If this is what you want to do, then you can run scripts when the system shuts down (or is hibernated or put to sleep) using the systemd infrastructure.

1
  • I dont want the system to shut down tho, just run the script. Feb 9, 2020 at 18:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .