1

I am trying to write some files in usb when connect it in linux..
Actually, I have a file in in /etc/udev/rules.d/ with:

ACTION=="add",  ENV{ID_FS_UUID}=="9FE8-99G3", RUN+="/usr/bin/sudo -u myuser /home/myuser/Scripts/mycrazy.sh"

In mycrazy.sh, I have:

## Path to mounted device
PATH_USB=/run/media/myuser/9FE8-99G3

mkdir $PATH_USB/some_folder

mkdir /home/myuser/Documents/another_folder

The first mkdir, doesnt works. But the second, works.
How can I write files in usb? Is missing something in my script?

2
  • It could be due to a small delay took by udisk to mount the partition after it does notice /dev/sdxY as your script does. But your script is so fast. Try putting a short delay at the beginning example sleep 3 , you may lower it if it does work
    – user.dz
    Jul 9, 2015 at 16:38
  • Nothing buddy.. Before the first mkdir I past the sleep.. Does not work. Jul 9, 2015 at 19:49

1 Answer 1

1
  • What works for me, adding delay (sleep) and disown the script that has delay using an intermediate script.

    RUN{type}

    ...
           This can only be used for very short-running foreground tasks.
           Running an event process for a long period of time may block all
           further events for this or a dependent device.
    
           Starting daemons or other long running processes is not appropriate
           for udev; the forked processes, detached or not, will be
           unconditionally killed after the event handling has finished.
    

    source: man udev

  • There a similar case here: Why do my udev rules run if I use udevadm trigger, but NOT at boot time?. Fëamarto's answer seems better than my solution here. It waits till its file system is ready in rw mode. You may give it a try.

    Because this is a USB drive, it can be removed before mounting. The script will stay alive till next reboot or it get plugged again (which trigger another instance) and mounted.

    One way to fix that is to check if related /dev/sdxY still exists. on each cycle.

  • Here is my setup:

    1. /etc/udev/rules.d/99-sneetsher-tests.rules

      ACTION=="add", ENV{ID_FS_UUID}=="6664-B2DA", RUN+="/usr/bin/sudo -u user /home/user/mycrazy.sh"
      
    2. /home/user/mycrazy.sh

      #!/bin/sh
      
      /home/user/mycrazy2.sh & disown
      
    3. /home/user/mycrazy2.sh

      #!/bin/sh
      
      PATH_USB=/media/user/MYFLASH3
      sleep 5
      mkdir $PATH_USB/some_folder
      

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .