python-evdev icon indicating copy to clipboard operation
python-evdev copied to clipboard

[SOLVED] Not getting any device when running as a daemon

Open HeuristicPerson opened this issue 1 year ago • 2 comments

I've created a python script that detects inactivity on gamepads and disconnects them from the system if they are bluetooth devices. The script is working fine with no problems when I run it as a regular user in Ubuntu.

Then, I created an user service with the following content:

[Unit]
Description=This service disconnects bluetooth gamepads after 10 minutes of inactivity.

[Service]
Type=simple
RemainAfterExit=true
ExecStart=python3 /opt/bluetooth_pad_disconnect/bluetooth_pad_disconnect.py

[Install]
WantedBy=default.target

The script starts with no problem but it doesn't detect any device at all. Do you have an idea of what the problem could be?

I've tried running a .bash script printing the user id and groups just in case there was a problem but everything is the same as if I was running a regular terminal.

HeuristicPerson avatar Feb 14 '24 22:02 HeuristicPerson

Does it work if you add a little bit of extra sleep to the beginning? Something like

import time
time.sleep(10)

You might want to consider writing an udev rule instead that runs the script for each individual bluetooth device, once they connect. Some hints on how this could work and debugged can be found here: https://github.com/sezanzeb/input-remapper/blob/main/data/99-input-remapper.rules

sezanzeb avatar Feb 17 '24 12:02 sezanzeb

It seems the problem was quite simple: Python's printing buffer, deactivating it when launching the scirpt, everthing is fine. What I didn't know until now is that the printing buffer can have a different size/behaviour when calling a script from terminal or as a service; the fact that I obtained different outputs didn't lead me to think about the buffer at all.

Thanks for the suggestion about using udev rules, @sezanzeb. I have a very limited understanding of Linux devices but the reason I opted for writing my own python script is that input devices ids can vary through time. My bluetooth pad can be device 11 now, but if I connect an USB pad before, it could be device 12 next time. By calling the service as a regular user (not as root), only devices connected by that user are obtained by list_devices, and then using some scripting I can detect which ones are bluetooth ones and can be deactivated after inactivity.

Regards

HeuristicPerson avatar Feb 18 '24 14:02 HeuristicPerson