问题
I'm trying to start a python script that waits for user input via a shell script triggered by a UDEV rule. After the input arrives the python script needs to make some database calls. I'm running into a couple different issues.
Here is the udev rule:
SUBSYSTEM=="usb" RUN+="/path/to/script.sh"
Issue #1 - I can't seem to get it to actually open the window. Here is what I have in script.sh:
#!/bin/bash
lxterminal -e /path/to/python_script.py
Here is the error I'm getting from udev:
'/path/to/script.sh'(err) '(lxterminal:4606): Gtk-WARNING **: cannot open display: '
Here is another version of the shell script which actually ran, but wasn't visible, and crashed when checking for input. Guessing because I'll need to re-route stdin if I ever get the terminal to open? Script:
export DISPLAY=0:; bash -c /path/to/script.sh
Error:
(err) name = input('Enter your name')
(err) 'EOFError: EOF when reading a line'
If I get rid of the input, I get this error:
[4859] exit with return code 0
This answer makes it sound like this isn't possible, but this is programming anything is supposed to be possible! https://unix.stackexchange.com/questions/119078/cannot-run-script-using-udev-rules
The end of this forum sounds like it's possible, but I'll need to use Zenity? https://ubuntuforums.org/showthread.php?t=759389
Any info from someone that's more familiar with udev would be great!
回答1:
For Issue #1;
lxterminal --command "python -i /path/to/python_script.py"
回答2:
I found the info I needed here: Scripts launched from udev do not have DISPLAY access anymore?
The script called by udev:
export DISPLAY=:0.0; export XAUTHORITY='/var/run/lightdm/root/:0'; /path/to/python.py
Then inside my python.py script:
cmd = ["zenity", "--entry", "--title='title'", "--text='{}'".format(var)]
response = (subprocess.check_output(cmd)).decode("utf-8") # Decode to a string
来源:https://stackoverflow.com/questions/39009749/open-an-lxterminal-window-via-shell-script-triggered-by-udev-rule