2

Is it possible to configure a systemd service to start or stop using a custom keyboard shortcut?

As an example, systemd has a special target "ctrl-alt-del.target" that allows a service to be bound to it. So I think there is a way to create my own systemd targets with custom keybindings but I was not able to find anything about it. In fact I need something "low level" that is able to launch a process (as root) whatever the running software stack (bash, xorg, fullscreen app etc..)

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
sgt-hartman
  • 271
  • 1
  • 10
  • With most window managers you can run arbitrary commands with key bindings, so you can start and stop processes. For example in [gnome](https://help.gnome.org/users/gnome-help/stable/keyboard-shortcuts-set.html.en), or [lxde](https://unix.stackexchange.com/questions/148501/how-to-add-a-key-binding-to-maximize-close-all-windows). You might need to use `sudo` to give yourself permissions. – Att Righ Nov 23 '17 at 14:13
  • Thanks but i would need a solution that works in a non graphical environment. – sgt-hartman Nov 23 '17 at 14:16
  • Umm, so the issue is you need *something* to catch the keybindings. I know that `zsh` can run arbitrary functions in response to keybindings. I don't use this feature in bash so I'm not so sure. I don't know about the kernels ability to handle keybindings. – Att Righ Nov 23 '17 at 14:19
  • Right. i'll edit my question to add more precisions – sgt-hartman Nov 23 '17 at 14:26
  • **ctrl-alt-del.target** So I was looking at this and it seems to be a magic built-in rather than a keybinding [1](https://www.freedesktop.org/software/systemd/man/systemd.special.html). I don't know how switching virtual terminals though. – Att Righ Nov 23 '17 at 14:33
  • So [loadkeys](https://linux.die.net/man/1/loadkeys) seems to handle the keybindings for the virtual console. [This question](https://unix.stackexchange.com/questions/146060/how-to-disable-alt-arrow-switching-of-virtual-consoles) talks about remapping some keys. – Att Righ Nov 23 '17 at 14:40
  • Thanks i'll take a look. Unfortunatelly theses keybindings seem to only concern virtual consoles. The `alt` + `arrow` keybinding given in example in the pointed so question doesn't work inside a xorg/wm session (but maybe i'm wrong). – sgt-hartman Nov 23 '17 at 14:56

1 Answers1

1

So, pretty convinced this feature didn't exist i sent a feature request on systemd github page: https://github.com/systemd/systemd/issues/7452

Quick answer was "won't implement" for pretty good reasons i suppose.

As a workaround a small daemonized script that listens to keyboard events should not be hard to implement. "inputexec" seems to simply do that but i have not tested it yet: https://github.com/rbarrois/inputexec

sgt-hartman
  • 271
  • 1
  • 10