Results 1 to 5 of 5

Thread: [SOLVED] Run script when USB Drive is plugged in

  1. #1
    Join Date
    Apr 2007
    Location
    Provo, UT
    Beans
    157
    Distro
    Ubuntu 8.10 Intrepid Ibex

    [SOLVED] Run script when USB Drive is plugged in

    With school starting again pretty soon and with the volume of programming I'll have to do for classes, I'm a little paranoid about losing all of my work. So, on my desktop at home I have a folder that every 30 minutes rsync with a flash drive that is always plugged into the back.

    What I want to do is have the computer recognize when I plug my other flash drive into the computer and rysnc it with the desktop. I know I could place the script in a cron and have it run really often just rysnc with the folder where the flash drive mounts, but, I'd rather be more elegant about it.

    Any ideas on how to have Gnome or Ubuntu run some script called, say, schbk.sh whenever I first plug it in? Similar to the autorun of Windows.
    ASUS M2A-MVP, AMD Athlon 64 X2 4000+, 3 Gb DDR2 800Mhz, GeForce 8600GT XXX Edition, Ubuntu 8.10

  2. #2
    Join Date
    Apr 2006
    Location
    Fresno CA
    Beans
    2,790
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Run script when USB Drive is plugged in

    I believe your solution will be in /etc/udev/rules.d. You can use rules to run scripts. In particular see 80-programs.rules.
    Thank a veteran -- George 8)
    Internet Coach & Writer
    Personal Blog -- 3 Joes' Blog

  3. #3
    Join Date
    Jun 2007
    Beans
    17,337

    Re: Run script when USB Drive is plugged in

    It would be very easy to do (100% automatic) if there is a way to 'override' the autorun prompt that hardy insists upon using.

    for example

    Put your script or a script calling your intended script into the root of the usb drive and name it autorun.sh. When you insert the drive you get a pop up asking if you want to run it, clicking 'run', runs the script(s).
    If there was a way to allow it to run without the prompt then that might fit the bill totally.
    Though clicking run on the pop up isn't too bad i guess.

    see screen

    I thought maybe if hardy saw the autorun.sh as being on a cdrom then it might allow autorun without a prompt. I created a small 'isofs' partition on the flash drive that only had an autorun.sh in it. While it was seen as a cdrom drive (/dev/scd2) it could only be automounted by an entry in fstab, requiring the drive being inserted at boot. Plus unfortunately this way also ran into the same 'prompt' step.

    Note; to get the popup I believe you have to go to file management -> behavior and enable 'run executable text....', otherwise it's shows up on r.click

    I found it easiest to leave my script in home dir. and just use this for the autorun (rip being name of my script
    #! /bin/sh
    cd ~
    ./rip
    Attached Images Attached Images
    Last edited by mc4man; August 30th, 2008 at 07:51 AM.

  4. #4
    Join Date
    Apr 2007
    Location
    Provo, UT
    Beans
    157
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Run script when USB Drive is plugged in

    I had no idea that .autorun would be recognized. Thanks for the tip - it worked perfectly!
    ASUS M2A-MVP, AMD Athlon 64 X2 4000+, 3 Gb DDR2 800Mhz, GeForce 8600GT XXX Edition, Ubuntu 8.10

  5. #5
    Join Date
    Jun 2007
    Beans
    17,337

    Re: [SOLVED] Run script when USB Drive is plugged in

    glad it worked for you.

    small point

    If you happened to set the 'executable text file' behavior in file management as noted above (to gain the pop up) there is a somewhat unintended consequence.
    If you have text files that were copied from somewhere (ex. a flash drive), then d. clicking on them will attempt to 'run' them . In most cases nothing happens including opening them to view. (hard to describe but if it occurs you'll know what I mean.

    The best way to approach is to r. click -> properties -> permissions and click twice on the execute box. First click will change from a line to a check mark, second click will clear the box (no execute)
    After that they will open as normal text files.

    Screen 1 before clicking, screen 2 after second click.
    Attached Images Attached Images

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •