Tiny Core Linux
General TC => Programming & Scripting - Unofficial => Topic started by: apuboard on November 23, 2015, 07: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"
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.
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.
-
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
-
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.
-
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).
-
But wouldn't it be possible to copy the script to /bin and execute it like every other , f.e.like mount?
-
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.
-
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
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)
#!/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:
#!/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?
-
RUN+="/bin/su -c '/bin/auto_import_relay.sh &'"
-
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
-
RUN+="/bin/su -c '/lib/udev/wrapper_for_your_script.sh %N %k %E &'"
/lib/udev/wrapper_for_your_script.sh
#!/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"
-
Thanks it worked like that. For what are these:%N %k %E?
-
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.