xkeysnail icon indicating copy to clipboard operation
xkeysnail copied to clipboard

Running in background

Open sensharma opened this issue 6 years ago • 8 comments

Hi mooz, am trying out xkeysnail and am loving it. Thank you!

I'd like to know if there can be a setup where this could run in the background, as a daemon, rather than on the terminal which needs to be kept open? Is there already an option to do that? If not, perhaps some flag like: sudo xkeysnail -d config.py to run it as a daemon in the background, with options to move it back to the foreground etc would be great!

Let me know your thoughts.

Thanks again.

sensharma avatar Mar 15 '18 00:03 sensharma

Is there a recommended way to start xkeysnail automatically when logging in? (I presume it may involve editing sudoers file to allow xkeysnail access to run as root)

Lenbok avatar May 12 '18 20:05 Lenbok

@Lenbok Hi. Do you find a method to do this? I failed after several methods.

yanghaoxie avatar Jun 13 '18 07:06 yanghaoxie

It's not a clean workaround, but running it in screen allows you to run it in the background.

sudo screen -dmS xkey /usr/local/bin/xkeysnail /home/yoda/config.py

pkkid avatar Jun 23 '18 23:06 pkkid

Here's the hacky method that I'm currently using, which seems to work to start xkeysnail when I log in. First one off configuration to allow starting xkeysnail via sudo without password:

echo "$USER ALL = (root) NOPASSWD: /usr/local/bin/xkeysnail" | sudo tee /etc/sudoers.d/xkeysnail

Then in my window manager autostart file (in my case with windowmaker it is $HOME/GNUstep/Library/WindowMaker/autostart), add:

sudo xkeysnail --watch ~/.xkeysnail.py &

I have no idea where the xkeysnail output is going. It would be nice if xkeysnail defaulted to less output anyway, as suggested in #26 (where it also sounds a nicer autostart system is possible via systemd)

Lenbok avatar Jun 27 '18 04:06 Lenbok

FWIW, I'm currently running xkeysnail as a systemd service with root user under AppArmor on Ubuntu 18.04 LTS.

[/etc/systemd/system/xkeysnail.service]

[Unit]
Description=Service for xkeysnail

[Service]
Environment=DISPLAY=:0
ExecStart=/usr/local/bin/xkeysnail /etc/xkeysnail/config.py
StandardOutput=null
Restart=always
Type=simple
RestartSec=10

[Install]
WantedBy=graphical.target

[/etc/apparmor.d/usr.local.bin.xkeysnail]

#include <tunables/global>

/usr/local/bin/xkeysnail {
  #include <abstractions/base>
  #include <abstractions/python>

  /usr/local/bin/xkeysnail r,

  /usr/local/lib/python3.*/dist-packages/xkeysnail/ r,
  /usr/local/lib/python3.*/dist-packages/xkeysnail/**.pyc mr,

  /etc/xkeysnail/config.py r,

  /dev/input/ r,
  /dev/input/* rw,
  /dev/uinput rw,
  /tmp/.X11-unix/X0 rw,

  /etc/passwd r,
}

[~/.xsessionrc]

xhost +SI:localuser:root

Originally I tried ExecStartPre=/usr/bin/xhost +SI:localuser:root in systemd file, but it does not work since root is not authorized to modify the access control at that point. And I rely on user's ~/.xsessionrc to add root and keep restarting xkeysnail with Restart=always until a user logged in. DISPLAY variable is also hardcoded in the systemd uit file.

It works for me, but I don't think it's generally appropriate to use the method above.

nobuto-m avatar Jun 30 '18 13:06 nobuto-m

I have no idea where the xkeysnail output is going. It would be nice if xkeysnail defaulted to less output anyway, as suggested in #26 (where it also sounds a nicer autostart system is possible via systemd)

You can redirect all (stdout and stderr) output to a file with: sudo xkeysnail config.py > ~/.xkeysnail.log 2>&1 &.

EDIT: This method does not always seem to be reliable as the log file is zero bytes, I tried it a few times. I got it working earlier but now I don't know why it broke down.

terop avatar Dec 29 '18 14:12 terop

My environment needs more capabilities. https://github.com/mooz/xkeysnail/issues/12#issuecomment-401541317

Env:

xkeysnail v0.1.0

❯ uname -a
Linux ken-ThinkPad-E490 5.0.0-13-generic #14-Ubuntu SMP Mon Apr 15 14:59:14 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

❯ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=19.04
DISTRIB_CODENAME=disco
DISTRIB_DESCRIPTION="Ubuntu 19.04"

❯ python3 -V
Python 3.7.3

❯ pip3 -V
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

Test: I ran command sudo apparmor_parser -r /etc/apparmor.d/usr.local.bin.xkeysnail; and systemctl restart xkeysnail.service; and journalctl until no apparmor="DENIED".

#include <tunables/global>

/usr/local/bin/xkeysnail {
  #include <abstractions/base>
  #include <abstractions/python>

  /usr/local/bin/ r,
  /usr/local/bin/xkeysnail r,
  /usr/bin/dash ix,
  /usr/bin/uname ix,

  /usr/local/lib/python3.*/dist-packages/** r,
  /usr/local/lib/python3.*/dist-packages/xkeysnail/**.pyc mr,

  /usr/share/dpkg/* r,

  /dev/input/ r,
  /dev/input/* rw,
  /dev/uinput rw,
  /tmp/.X11-unix/X0 rw,
  /proc/*/mounts r,
  /proc/*/fd/ r,

  /etc/passwd r,
  /etc/nsswitch.conf r,
  /etc/default/apport r,
  /etc/apt/** r,

  /etc/xkeysnail/*.py r,
}

ref. https://wiki.ubuntu.com/AppArmor

ksoda avatar Apr 30 '19 14:04 ksoda

See https://github.com/mooz/xkeysnail/issues/64#issuecomment-600380800 for how to run xkeysnail without sudo (so you can just put it in your regular startup scripts, and also allows the launch functionality to work as expected to launch apps as the actual user).

Lenbok avatar Mar 18 '20 01:03 Lenbok