Announcement

Collapse
No announcement yet.

Can't get figure out how to use udev to launch scripts!

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [SOLVED] Can't get figure out how to use udev to launch scripts!

    ### Problem ###

    I'm usually comfortable around my system but I've come across a problem that I just can't figure out. I have a server which runs headless that handles a samba share for the house. The only issue I have is that my brother occasionally unplugs the device. At first I just used to use an fstab entry but that only helps at boot time and isn't ideal as the system is ocasionally booted without the harddrive. My idea is now to launch a script using udev rules mount the drive. I've been running into issues and have reduced my problem to as basic a form as is possible and am now testing it on my own system and I still can't solve it.

    ### My attempt ###

    My first step was to create the file /etc/udev/rules.d/03-mountNex.rules:
    Code:
    ERNEL=="sdc", ACTION=="add", RUN+="/home/daniel/script"
    This should launch the script "/home/daniel/script" when I plug in a third hard drive.

    The script itself is for now rudimentary:
    Code:
    #! /usr/bin/sh
    /usr/bin/dolphin
    The script just opens up an instance of dolphin. Nothing fancy.

    I now restart my computer with the device not plugged in. Login in and then plug the device in.

    Some other useful info is my permissions on the udev file and script:

    Code:
    daniel@linux-w365:/etc/udev/rules.d> ls -l
    total 292
    -rw-r--r-- 1 root root     57 Jan  5 16:36 03-mountNex.rules
    ........

    Code:
    ls -l                                                                                                           
    total 17400                                                                                                                          
    ........
    -rwxr-xr-x  1 daniel users          32 Jan  5 16:28 script
    ........
    ### Intended Result ###
    When I plug the device in then dolphin should launch

    ### Actual Result ###
    Absolutely nothing happens except the normal KDE dialog pop-ups asking what I want to do with the drive.

    ### Suspected issue ###
    Something to do with permissions but I'm not sure.

    ### System Info ###
    Kubuntu 12.04 with Kubuntu-Backports ppa and latest available KDE.


    Thanks a ton for any help you can provide.

    #2
    Originally posted by dmeyer View Post
    Code:
    ERNEL=="sdc", ACTION=="add", RUN+="/home/daniel/script"
    That should be KERNEL

    Also, udev rules run as root and are not attached to a x session so launching gui programs is a little more complicated, try this as the script to make sure the udev rule is working:
    Code:
    #!/bin/bash
    echo "It Works $(date +%F-%H-%M)" >> /tmp/udev.test
    What is your end goal? What do you want the udev rule to do?
    Last edited by james147; Jan 05, 2013, 09:50 AM.

    Comment


      #3
      Originally posted by james147 View Post
      That should be KERNEL
      Sorry I just copy and pasted poorly. It does indeed say KERNEL.

      Originally posted by james147 View Post
      Also, udev rules run as root and are not attached to a x session so launching gui programs is a little more complicated, try this as the script to make sure the udev rule is working:
      Thanks, this was the magic bit of information I needed. I followed so many tutorials etc and I just kept running into issues. I can't believe it was this straightforward. I don't need to run a gui program, what I actually need to do is mount a hard drive. Thanks a ton for all the help! Now everything is running.

      Comment


        #4
        You know that kde can auto mount hard drives if you tell it to?

        Comment


          #5
          FYI: here is the udev rule I was using to mount a backup drive and run a backup script, it automounts any USB drive with the label "Backup", creates a mount point and mounts the device then runs a backup script. On removal it deletes the mount point.

          Code:
          KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"
          
          # Import FS infos
          IMPORT{program}="/sbin/blkid -o udev -p %N"
          
          
          # Get a label if present, otherwise specify one
          ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
          ENV{ID_FS_LABEL}=="", GOTO="media_by_label_auto_mount_end"
          ENV{dir_name}!="Backup", GOTO="media_by_label_auto_mount_end"
          
          
          # Global mount options
          ACTION=="add", ENV{mount_options}="relatime,sync"
          # Filesystem-specific mount options
          ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"
          
          
          # Mount the device
          ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"
          
          
          # Clean up after removal
          ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"
          
          
          # Run backup script
          ACTION=="add", RUN+="/usr/local/bin/backup-nae /media/%E{dir_name}" 
          
          
          # Exit
          LABEL="media_by_label_auto_mount_end"

          Comment


            #6
            Originally posted by james147 View Post
            You know that kde can auto mount hard drives if you tell it to?
            True but I'm actually just testing this script on my own PC before I migrate it to a headless server without an X session running. KDE won't be very helpful there.

            I was just struggling to understand the way udev processes the rules, the limitations of the scripts and the permissions etc needed. I understand it now, and in fact the udev rule links to a script that does a bunch of operations like mount the drive, run rsync for a back-up, email me that it was connected with details and a few other things.

            I also used ATTRS{<something>}=="<something>" to uniquely identify the drive rather than KERNEL=="sdc".

            Thanks for posting an example. I actually learnt a lot from dissecting it and I'm definitely going to use more udev rules!

            Comment


              #7
              Originally posted by dmeyer View Post
              I also used ATTRS{<something>}=="<something>" to uniquely identify the drive rather than KERNEL=="sdc".
              I prefer to use the drive label, that way you can replace the drive without updating the server configs. Or have multiple drives.

              Comment


                #8
                Thanks for the tip. I also see the thread is marked as solved already so nothing left for me anymore. Seriously though, thanks a ton for your help.

                Comment

                Working...
                X