LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   SUSE / openSUSE (https://www.linuxquestions.org/questions/suse-opensuse-60/)
-   -   Udev fails running python script (https://www.linuxquestions.org/questions/suse-opensuse-60/udev-fails-running-python-script-941717/)

szzed 04-25-2012 02:29 PM

Udev fails running python script
 
Hi All!
I have a PC openSuSE 12.1 base and X11 installed on it. I use icewm as windowmanager. I wrote some udev rules that finds the attached USB keys, and runs a bash script. That bash script then starts a python script, that implements GUI interface for clamav, with Tkinter. The problem is that, the python script doesn't start clamav_scan. If I run the first bash script from console, everything works perfectly, but if it's started by udev, the clamav_scan doesn't start, but the bash and python script runs.
Is there a special environment, where the udev runs, and this causes the failure? Or what else can be the problem?

Thanks for replies!

Erfankam 04-25-2012 03:16 PM

try use root privileges in your udev rules. I dont remember how could you use root privileges in udev. Use google. Also take a look at gksudo to use. Maybe you have not sufficient privilege to invoke gui from a scripts.
Thus Do proper actions by graphical root privileges.
I hope it be useful for you.

catkin 04-25-2012 09:04 PM

udev runs as root and does not, by default, have access to desktop sessions.

Two things need to be done:
  1. The desktop user needs to give permission for other users on the local host to access their desktop.
  2. The script needs the DISPLAY variable set.

For the first, one way for bash users to do it is to have this in ~/.bash_profile:
Code:

[[ $DISPLAY ]] && /usr/bin/xhost +localhost
For the second, on a single user system (so only one X display running) the script needs something like this:
Code:

export DISPLAY='localhost:0.0'

szzed 04-26-2012 01:22 AM

wow, thanks for the fast replies!
Both of you are true, but my problem is not with the gui. The gui works fine, even if it's called from udev, but the clamav_scan program is not called from python.
So, I plug in my USB key, udev realizes that, successfully starts the first bash script. That script mounts the new drive into a temporary directory, and starts the python script. The python script starts running, the window appears. At this point, the clamav_scan should start, and should scan the directory (the mounted USB key), and should display the results. But this last two steps never happens. Just these last two steps.
If I mount the USB key manually, into the same temporary directory, and I start the bash script also manually, the python window appears, the clamav_scan starts scanning, and in the end, the window displays the results.
Normally, the scanning lasts about 10 seconds (the key has 1 partition, ext3 fs, and 1, 342 byte sized file), and I've already waited for udev to scan the key for half an hour, but it didn't scan.

catkin 04-26-2012 01:44 AM

When I tested the environment set by udev in a script run by udev the PATH was limited. It may be that your bash script is robust against that but the python script is not. It would do no harm to set the envars the scripts depend on in the scripts themselves. At least PATH and DISPLAY. Running env at the command prompt may suggest others.

Erfankam 04-27-2012 03:27 AM

Maybe catkin s right. but I suggest test your python program whether knowing proper path to look for a device. Also you can use 'findfs' tool to find your device mount point based on UUID or lable. You can pass UUID and mount point of new device as some arguments to your python script. I think there s some issue in your script.
Regards.

szzed 04-30-2012 12:02 PM

Again Thank you all for the fast replies!
I found something strange in my code. I started the python script with
Code:

su -c 'export DISPLAY=:0;xhost local:$USER;/usr/local/sbin/scandrive.py'
code from the bash script.
This started the beginning of the python code (showed the GUI), but it didn't refer the other functions in the file. I changed the above code to
Code:

su $USER -c 'export DISPLAY=:0; xhost local:$USER;/usr/local/sbin/scandrive.py'
, and the script ran as I expected.
So, thank you very much again!

catkin 04-30-2012 09:36 PM

Glad you solved it and thanks for sharing the solution :)


All times are GMT -5. The time now is 02:49 AM.