是否可以使用 systemd 在按下电源按钮时运行脚本?

是否可以使用 systemd 在按下电源按钮时运行脚本?

我环顾四周,没有发现任何东西,就我所见,人们总是对什么感到满意登录配置文件正在提供,这是有趣的部分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.

然后我在这里重复有趣的部分:

控制登录应如何处理系统电源和睡眠键以及盖子开关以触发系统断电或挂起等操作。可以是“忽略”、“断电”、“重新启动”、“停止”、“kexec”、“挂起”、“休眠”、“混合睡眠”和“锁定”之一。

或者我的方式错误,这只是键盘按键,而不是电源按钮?

无论如何,以前用acpi很容易,只需更换电源按钮中的脚本/usr/lib/acpid/,是否有与 systemd 等效的东西?

注意(重要)如何使用 systemd 在按下键盘电源键时运行脚本?不是重复的,因为已被错误地标记为重复如何更改电源按钮关闭操作以在 systemd 下运行脚本 that DOES NOT answer my question, since this is to manage power key from keyboard, not power button:

电源按钮

正如 @TooTea 所建议的,集成到外壳中的按钮可能被视为键盘按钮按下,无论如何,在检查后我没有这样的 /dev/input/by-path/platform-i8042-serio-0- event-kbd 文件来监视按下的按键,那么它绝对不能回答我的问题。

答案1

最后我找到了一个解决方案,但很幸运,因为我使用的是 openbox:

在我的例子中,在 LXDE 下,编辑~/.config/openbox/lxde-rc.xml并在<keyboard> ... </keyboard>部分中添加:

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

例如,对于我的测试,我只是打开一个弹出窗口,显示“按下电源关闭”:

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

然后在终端类型中openbox-lxde --reconfigure,按塔式机箱电源按钮,将显示以下消息:

在此输入图像描述

编辑:我忘了提及(但不确定这是强制性的),我将系统关闭按钮设置为空,要检查这一点,请转到“开始菜单”->“系统”->“首选项”->“电源管理器” ,并确保“按下电源按钮时”设置为“不执行任何操作”:

在此输入图像描述

相关内容