LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   udev and dbus - using the notivy-send command in a script started by udev (http://www.linuxquestions.org/questions/slackware-14/udev-and-dbus-using-the-notivy-send-command-in-a-script-started-by-udev-4175432121/)

markush 10-14-2012 06:40 AM

udev and dbus - using the notivy-send command in a script started by udev
 
Hello together,

I'm lost with this issue. Referring to this thread I've written an udev-rule which recognizes the plugin of an USB-memory-stick and calls a script which mounts the device. So far so good.

Here's the script:
Code:

#!/bin/bash

# USB-Stick mounten
sleep 5
device=`dmesg | grep -e sd.: | tail -n 1 | sed -n 's/.*sd.:\s\+\?\(.*\)/\1/i p'`
name=`dmesg | grep -i product: | tail -n 1 | sed -n 's/.*product:\s\+\(.*\)/\1/i p' | sed -n 's/\ /_/g p'`


if [ ! -d /media/$name ]; then
                                mkdir /media/$name
fi
mount /dev/$device /media/$name
#for LINE in $(find /proc/ -maxdepth 2 -name environ -exec grep -z "^DBUS_SESSION_BUS_ADDRESS" {} \; | sort -uz | tr '\0' '\n')
#do
#                                eval $LINE
#done
#username=`who | head -n 1 | cut -d " " -f 1`
#su $username -c notify-send "USB-Speicherstick eingehängt auf /media/$name"
echo "USB-Speicherstick eingehaengt auf /media/$name" | wall

Additionally I want to receive a message about where the device is mounted.

I've tried the "notify-send" command which works when I call the script on the commandline (as root as well as a normal user). The problem is that the message is not shown when the script is called from udev. I searched the internet and found that the problem is with dbus. What I have understood is that the script, when called from udev doesn't know where to display the message. The reason is that the script needs additional information about a (some?) variables from dbus. There are solutions explained in several places in the internet, but I did not understand one of them.

Does anyone know which variable (information from dbus) the notify-send command needs and which way I can provide this information to the command. There's no manpage vor notify-send and the helptext for the command doesn't provide as much information. As you can see in the script I've tried out various ideas. using "su -c username" doesn't work, and I did not understand what to do with DBUS_SESSION_BUS_ADDRESS.

I've also found udev-notify at slackbuilds.org, but could not find out if an how this tool could help me.

Markus

phenixia2003 10-14-2012 08:23 AM

Hello,

I encountered the same kind of problem when I wanted to add a notification mechanism to compat32pkg. I fixed that problem
by exporting the variable DISPLAY. So, in your case, you should simply have to change your code as below:

Code:

export DISPLAY=:0.0 su $username -c notify-send "USB-Speicherstick eingehängt auf /media/$name"
Greetings.

--
SeB

markush 10-14-2012 10:26 AM

Thanks very much to phenixia2003, I've got it working now, here's the script:
Code:

#!/bin/bash                                                                                                                                                           
                                                                                                                                                                     
# USB-Stick mounten                                                                                                                                                   
sleep 5                                                                                                                                                               
device=`dmesg | grep -e sd.: | tail -n 1 | sed -n 's/.*sd.:\s\+\?\(.*\)/\1/i p'`                                                                                     
name=`dmesg | grep -i product: | tail -n 1 | sed -n 's/.*product:\s\+\(.*\)/\1/i p' | sed -n 's/\ /_/g p'`                                                           
                                                                                                                                                                     
if [ ! -d /media/$name ]; then                                                                                                                                       
    mkdir /media/$name                                                                                                                                               
fi                                                                                                                                                                   
mount /dev/$device /media/$name                                                                                                                                       
username=`who | head -n 1 | cut -d " " -f 1`                                                                                                                         
export DISPLAY=:0.0                                                                                                                                                   
su $username -c "notify-send 'USB-Speicherstick eingehaengt auf /media/$name'"

The " and ' musst be where they are because otherwise the $name-variable isn't correctly evaluated.

Markus


All times are GMT -5. The time now is 06:00 AM.