WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: How to use an udev rule to execute script when usb is plugged in  (Read 2554 times)

Offline apuboard

  • WikiUser
  • *
  • Posts: 30
How to use an udev rule to execute script when usb is plugged in
« on: November 23, 2015, 04:41:46 AM »
A script, which is located on the usb stick should be executed when the stick is plugged in.
That's how it looks like. As much as I know everything is working except from the snippet I wrote.

This is the part which should execute a tcppdump script:
RUN+="/media/%E{dir_name}/autotcpdump.sh"


Code: (bash) [Select]
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}=="", ENV{dir_name}="usbhd-%k"
# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# 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}", RUN+="/media/%E{dir_name}/autotcpdump.sh"
# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"
# Exit
LABEL="media_by_label_auto_mount_end"

And that's the script, the while-clause is not working yet, but I'm currently using it for the echo "worked" to debug.

Code: (bash) [Select]
tc@box:/media/usbhd-sdb1$ cat autotcpdump.sh
#!/bin/sh
sdb=$(sudo cat /proc/mounts | grep sd[b-d]1 | cut -d "/" -f3)
sudo echo "worked" | sudo tee -a /media/usbhd-sd[b-d]1/test.txt
while [ "$sdb" == "sdd1" ]
do
        sudo tcpdump -i eth0 -w /media/usbhd-sdd1/abfrage2.pcap -c10
        echo "ok" >> /media/usbhd-sdd/test.txt
done

I can execute autotcpdump manually but, so I think the fault is inside the rule.
« Last Edit: November 23, 2015, 04:43:31 AM by apuboard »

Offline Misalf

  • Hero Member
  • *****
  • Posts: 1703
Re: How to use an udev rule to execute script when usb is plugged in
« Reply #1 on: November 23, 2015, 08:48:13 AM »
You can't run your script directly from within the udev rule.
Here is how got I something similar working.
http://forum.tinycorelinux.net/index.php/topic,18531.msg113305.html#msg113305
Download a copy and keep it handy: Core book ;)

Offline apuboard

  • WikiUser
  • *
  • Posts: 30
Re: How to use an udev rule to execute script when usb is plugged in
« Reply #2 on: November 23, 2015, 10:28:26 PM »
So as I much as I now, it's possible to run an other script which is located in /home and execute the one on the usb drive.
I think at the time I execute the script, the usb drive isn't mounted yet. How do you mean that I can't run my script? Because it's on the drive?
You can't run your script directly from within the udev rule.
 

Offline Misalf

  • Hero Member
  • *****
  • Posts: 1703
Re: How to use an udev rule to execute script when usb is plugged in
« Reply #3 on: November 24, 2015, 01:07:35 AM »
I'm far from being an expert on udev so my wording might be quite bad.
How I get it is that udev doesn't run in the same space as the usual apps so it needs extra steps for executing things to be used by a normal user (i.e.  su -c  and, possibly only for GUI apps, also  DISPLAY=:0.0).
Download a copy and keep it handy: Core book ;)

Offline apuboard

  • WikiUser
  • *
  • Posts: 30
Re: How to use an udev rule to execute script when usb is plugged in
« Reply #4 on: November 24, 2015, 01:54:35 AM »
But wouldn't it be possible to copy the script to /bin and execute it like every other , f.e.like mount?

Offline Misalf

  • Hero Member
  • *****
  • Posts: 1703
Re: How to use an udev rule to execute script when usb is plugged in
« Reply #5 on: November 24, 2015, 02:03:32 AM »
Well, yes and no. You can execute anything from udev but it doesn't know about your $PATH.
You would probably need to use the full path for any command used in your script and you could omit  sudo  as it would run with root privileges anyway.
Download a copy and keep it handy: Core book ;)

Offline apuboard

  • WikiUser
  • *
  • Posts: 30
Re: How to use an udev rule to execute script when usb is plugged in
« Reply #6 on: November 24, 2015, 02:11:51 AM »
So I made some changes, this is how my udev rule is currently looking. The Action on top gets executed but the auto_import_relay.sh doesn't work

Code: (bash) [Select]
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{
ACTION=="add", RUN+="/bin/auto_import_relay.sh"

This is my auto_import_relay(When I execute it it doesn't exit)
Code: (bash) [Select]
#!/bin/sh
/media/usbhd-sdb1/test.sh & exit
echo "auto_import_relay worked"
And then it should execute a script on the usb stick.

That's the script that should get executed:

Code: (bash) [Select]
#!/bin/sh
sudo echo "workedtest" | sudo tee -a /media/usbhd-sd[b-d]1/test.txt


But what your saying is that I have to write things like DISPLAY=:0.0?


« Last Edit: November 24, 2015, 02:13:54 AM by apuboard »

Offline Misalf

  • Hero Member
  • *****
  • Posts: 1703
Re: How to use an udev rule to execute script when usb is plugged in
« Reply #7 on: November 24, 2015, 02:15:05 AM »
Code: [Select]
RUN+="/bin/su -c '/bin/auto_import_relay.sh &'"
Download a copy and keep it handy: Core book ;)

Offline apuboard

  • WikiUser
  • *
  • Posts: 30
Re: How to use an udev rule to execute script when usb is plugged in
« Reply #8 on: November 24, 2015, 02:29:05 AM »
Still didn't work do you know some good ways to debug it. I already used udevadm monitor --property but I couldn't find anything that helps

Offline Misalf

  • Hero Member
  • *****
  • Posts: 1703
Re: How to use an udev rule to execute script when usb is plugged in
« Reply #9 on: November 24, 2015, 02:41:46 AM »
Code: [Select]
RUN+="/bin/su -c '/lib/udev/wrapper_for_your_script.sh  %N %k %E  &'"

/lib/udev/wrapper_for_your_script.sh
Code: [Select]
#!/bin/sh
TCUSER=`/bin/cat /etc/sysconfig/tcuser`
PARAMS="$@"
/bin/su -c "/bin/echo $PARAMS >> /tmp/test.txt"  - "$TCUSER"
/bin/su -c "/bin/sleep 1 ; /bin/auto_import_relay.sh" - "$TCUSER"

Download a copy and keep it handy: Core book ;)

Offline apuboard

  • WikiUser
  • *
  • Posts: 30
Re: How to use an udev rule to execute script when usb is plugged in
« Reply #10 on: November 24, 2015, 04:06:19 AM »
Thanks it worked like that. For what are these:%N %k %E?

Offline Misalf

  • Hero Member
  • *****
  • Posts: 1703
Re: How to use an udev rule to execute script when usb is plugged in
« Reply #11 on: November 24, 2015, 04:50:29 AM »
That's from the udev rule you posted.
You could ship these parameters to your script and let it handle the creation of directories and such, in case that fails using udev.
Download a copy and keep it handy: Core book ;)