4

I looked around and didn't found anything on this, for what I've seen, people always have been satisfied with what logind.conf is offering, here is the interesting part of man logind.conf:

HandlePowerKey=, HandleSuspendKey=, HandleHibernateKey=, HandleLidSwitch=, HandleLidSwitchDocked=
       Controls how logind shall handle the system power and sleep keys and the lid switch to trigger actions such as system power-off or suspend. Can be one of "ignore", "poweroff", "reboot", "halt", "kexec", "suspend", "hibernate", "hybrid-sleep", and "lock". If "ignore", logind will never handle these keys. If
       "lock", all running sessions will be screen-locked; otherwise, the specified action will be taken in the respective event. Only input devices with the
       "power-switch" udev tag will be watched for key/lid switch events.  HandlePowerKey= defaults to "poweroff".  HandleSuspendKey= and HandleLidSwitch= default to
       "suspend".  HandleLidSwitchDocked= defaults to "ignore".  HandleHibernateKey= defaults to "hibernate". If the system is inserted in a docking station, or if
       more than one display is connected, the action specified by HandleLidSwitchDocked= occurs; otherwise the HandleLidSwitch= action occurs.

       A different application may disable logind's handling of system power and sleep keys and the lid switch by taking a low-level inhibitor lock
       ("handle-power-key", "handle-suspend-key", "handle-hibernate-key", "handle-lid-switch"). This is most commonly used by graphical desktop environments to take
        over suspend and hibernation handling, and to use their own configuration mechanisms. If a low-level inhibitor lock is taken, logind will not take any action
       when that key or switch is triggered and the Handle*= settings are irrelevant.

I then repeat the interesting part here:

Controls how logind shall handle the system power and sleep keys and the lid switch to trigger actions such as system power-off or suspend. Can be one of "ignore", "poweroff", "reboot", "halt", "kexec", "suspend", "hibernate", "hybrid-sleep", and "lock".

Or I am in the wrong way and this is just for keyboard keys, and not power button?

In any case, previously, it was easy with acpi, one just had to replace the power_button script in /usr/lib/acpid/, isn't there something equivalent for systemd ?

NB (IMPORTANT): How can I run a script on keyboard power key press with systemd? is NOT a duplicate since has been wrongly marked as duplicate of How to change Power button shutdown action to run a script under systemd that DOES NOT answer my question, since this is to manage power key from keyboard, not power button:

Power button

And as suggested by @TooTea it may be true that button integrated into the case is seen as a keyboard button press, anyway, after having checked I have no such /dev/input/by-path/platform-i8042-serio-0-event-kbd file to monitor key pressed, then it definitively does not answer my question.

5
  • Thanks @Isaac but as I already mentioned in the "NB", it does not.
    – gluttony
    Jan 27, 2020 at 8:47
  • 1
    @gluttony I think you should explain in more detail why the question suggested by Isaac isn't relevant. To me, both answers to that question seem directly applicable to this question. Note that there's really no functional difference between a button integrated into your keyboard and a button integrated into the case. The latter is typically presented by the system as an input device ("keyboard" with a total of one key).
    – TooTea
    Jan 30, 2020 at 8:12
  • @TooTea: thanks for your answer, with this additional information I can look at it since I didn't know that button integrated into the case is seen as a keyboard button press, anyway, after having checked I have no such /dev/input/by-path/platform-i8042-serio-0-event-kbd file to monitor key pressed, then it definitively does not answer my question.
    – gluttony
    Feb 4, 2020 at 15:27
  • Does this answer your question? How to change Power button shutdown action to run a script under systemd
    – sam
    Feb 6, 2020 at 7:21
  • Thanks @sam but already proposed and does not answer my question, it is in my "NB (IMPORTANT)" in my question.
    – gluttony
    Feb 6, 2020 at 8:14

1 Answer 1

1

Finally I found a solution, but by luck because I'm using openbox:

And in my case under LXDE, then, edit ~/.config/openbox/lxde-rc.xml and in <keyboard> ... </keyboard> section add:

  <keybind key="XF86PowerOff">
      <action name="Execute">
        <command>command or script to run</command>
      </action>
  </keybind>

For example for my test I just open a popup saying "Power off pressed":

  <keybind key="XF86PowerOff">
      <action name="Execute">
        <command>zenity --info --text="Power off pressed"</command>
      </action>
  </keybind>

Then in a terminal type openbox-lxde --reconfigure so that it is taken into account, press your tower case power button and the following message will appear:

enter image description here

Edit: I forgot to mention (but not sure this is mandatory), I have system shutdown button set to nothing, to check this, go to "Start menu" -> "System" -> "Preferences" -> "Power Manager", and ensure that "When power button is pressed" is set to "Do nothing":

enter image description here

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.