Russed
Posts: 4
Joined: Wed Sep 24, 2014 10:34 pm

UDEV rules

Sat Jan 10, 2015 2:12 pm

I am just starting work on a project and using python in an effort to learn it. The basic idea is on add a USB files get copied and on removal the pi reboots. I first wrote the udev rule for add.

/etc/udev/rules.d/999-usb.rules

Code: Select all

KERNEL=="sd*1",ACTION=="add",RUN=="/home/pi/usb.sh"

the usb.sh

Code: Select all

		#!/bin/bash
		cd /
		cd /home/pi/
		sudo python upload.py
		cd /
		exit
the python

Code: Select all

		#!/usr/bin/python
		
		from os import walk
		import os
		
		os.system("sudo reboot")
and it worked, i place in a usb in and the sytem reboots ( this was so i colud make sure it worked) i added the part of the script to copy the contents

Code: Select all

#!/usr/bin/python
		
from os import walk
import os
import shutil
src_files = os.listdir('/media/David/videos')
for file_name in src_files:
    full_file_name = os.path.join('/media/David/videos', file_name)
    if (os.path.isfile(full_file_name)):
        shutil.copy(full_file_name, '/home/pi/videos/')
print ("i'm done")

and tested it with "sh usb.sh" in a terminal window. worked perfectly however on plugging in the usb nothing. i have looked and i think udev runs as root so1 i'm not sure why its not working. i took the code back to one line and it works.
( the print was so i can see it a terminal window when it finishes this will be a message box with Tkinter)

User avatar
PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

Re: UDEV rules

Sat Jan 10, 2015 2:17 pm

There is another approach, and that is to use pyudev to set up a listener for the events from udev.

I've done stuff with the "media change" events from cdrom drives to detect when disks are inserted and removed. I'm sure you can do the same with USB stick insertion and removal.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

hunternet93
Posts: 336
Joined: Mon Dec 12, 2011 4:34 pm

Re: UDEV rules

Sun Jan 11, 2015 1:01 am

Try changing your udev rule to:

Code: Select all

KERNEL=="sd*1",ACTION=="add",RUN=="sh /home/pi/usb.sh &> /tmp/script-log"
The "&>" symbol tells the shell to redirect the script's output and error messages to the file /tmp/script-log, once the script runs then any error messages should appear in that file, which you can read with your text editor of choice (i.e nano, vi, leafpad). If the file doesn't exist after plugging your USB drive then the script isn't running for some reason.

Russed
Posts: 4
Joined: Wed Sep 24, 2014 10:34 pm

Re: UDEV rules

Sun Jan 11, 2015 10:14 am

Thanks hunternet93 and PeterO, i have tried adding the log and nothing was shown so it is not working. I have double checked today and the UDEV does run the shell script but stops when it gets to the python so i am now looking at pyudev, never heard of it so should be fun to learn. when i get it working i will post a reply.

User avatar
topguy
Posts: 6641
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: UDEV rules

Sun Jan 11, 2015 12:22 pm

Two things I would change in "usb.sh".

* Not use "sudo", you should be root anyway.
* Use full path to "python" binary. ( /usr/bin/python or something )

Return to “General discussion”