2

How does one change the power button shutdown action under say debian 10 systemd to run a custom script instead of shutting down the system?

Back when this was handled by acpi one could edit the scripts in /etc/acpi. The only suggestion I have found thus far is to replace various power related binaries with my script.

Is there a way to do this without replacing binaries?

2

It depends on what you want to do.

  • If you just need to temporarily block poweroff while you're running some operation, see systemd-inhibit. You can also block sleep, suspend, etc. like this. You can also temporarily (as long as your program is running) tell systemd to ignore the power button, intended to let some other program act on it instead.

  • If you just want to run some extra code before shutdown, then you can have your service started by poweroff.target. You may also want to consider shutdown.target and reboot.target. (I think you could also change the poweroff.target to not pull in systemd-poweroff.service if you want to change entirely what the power button does — but beware things like systemctl poweroff and poweroff go through that too).

  • If you want to disable systemd's handling of the power button entirely, you'll first have to know it's actually logind that normally does it. The HandlePowerKey option in /etc/systemd/logind.conf lets you disable it (by setting to ignore). You could then have some other program listen for the keys.

| improve this answer | |
  • I would like to run a custom script via the power button. I suspect the answer to the original post is hidden in "some other program listen for keys". – user3718260 Dec 6 '19 at 23:47
2

Not really the solution I was hoping for, but here is what I ended up doing:

Setting

HandlePowerKey=ignore

in /etc/systemd/logind.conf does remove the power key handling from systemd.

After that you are left with handling the power button like any other key.

In my case, I can pickup the keypress event from

/dev/input/by-path/platform-r_gpio_keys-event

as described in this question Accessing Keys from Linux Input Device

I created a modified version of the example program in the linked question to wait for my power key, which is keycode 116, and then run my script.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.