texy
Forum Moderator
Forum Moderator
Posts: 5156
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

auto mounting usb stick and running a script

Fri Apr 08, 2016 9:21 am

Hi,
for a pi based project at work I am trying to do the following :
1..automount ANY usb stick
2..run a script that copies particular files ( if found ) from the usb stick to the boot directory

So far I have used usbmount to auto mount the usb stick and this is working OK - when mounted the files can be found at the default directory
/media/usb/
The udev rule is

Code: Select all

KERNEL=="sd*[!0-9]", ACTION=="add", RUN+="/home/pi/usbcopy.sh"
The usbcopy.sh script is

Code: Select all

#!/bin/bash
sleep 5
ls -lt /media/usb/ > /home/pi/dir.txt
sudo cp /media/usb/thermo*.py /boot/
ls -lt /boot/thermo*.py > /home/pi/usbcopy.log
I know the udev rule is triggered and the script is run because the dir.txt and usbcopy.log files are created, however both are empty, so the file copying is not taking place.
I think it could be that the udev rule is actioned before usbmount has done it's stuff. I was thinking that I should do away with usbmount and use separate udev rules for mounting/unmounting. However I would prefer to disable usbmount whilst I experiment, rather than remove it altogether. Any idea how to do that, or any other suggestions on getting this working?

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: auto mounting usb stick and running a script

Fri Apr 08, 2016 9:27 am

See viewtopic.php?f=63&t=125495

You can't directly start long-running processes with udev RUN+=

Your bash script will be killed by the end of that sleep 5...

texy
Forum Moderator
Forum Moderator
Posts: 5156
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: auto mounting usb stick and running a script

Fri Apr 08, 2016 9:42 am

OK thanks, but I did only add that sleep command recently - it still didn't work without it (I will remove it again).
That thread you pointed to states that the udev script must be very short time-wise, but it's OK to call another script from the initial script as that is treated separately?
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

texy
Forum Moderator
Forum Moderator
Posts: 5156
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: auto mounting usb stick and running a script

Fri Apr 08, 2016 9:57 am

I changed the udev rule to

Code: Select all

KERNEL=="sd*[!0-9]", ACTION=="add", RUN+="/home/pi/udev.sh"
udev.sh is

Code: Select all

#!/bin/bash
setsid /home/pi/usbcopy.sh
and usbcopy.sh is now

Code: Select all

#!/bin/bash
dir /media/usb/ > /home/pi/dir.txt
sudo cp /media/usb/thermo*.py /boot/
ls -lt /boot/thermo*.py > /home/pi/usbcopy.log
No joy. As the generated dir.txt file is empty, I still think it's because usbmount hasn't had time to do it's stuff. If manually dir right after plugging in the stick it takes a few attempts, ie it takes a few seconds, until it is found and read. I can't find any documentation on usbmount it would be useful to temporarily disable it.
Failing that I can just remove it......
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
DougieLawson
Posts: 35381
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: auto mounting usb stick and running a script

Sat Apr 09, 2016 8:39 am

I run a script for USB flash drives with this

Code: Select all

KERNEL=="sd*", SUBSYSTEMS=="scsi", DRIVERS=="sd", SYMLINK+="flash",  RUN+="/usr/local/bin/flash_drive $kernel'"
The basic version runs this

Code: Select all

#!/usr/bin/python
import sys
import logging
import logging.handlers

my_logger = logging.getLogger('Flash_drive')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address = '/dev/log')
my_logger.addHandler(handler)

my_logger.debug('Flash drive active')
my_logger.debug('Argv[0]:'+sys.argv[0])
try:
  my_logger.debug('Argv[1]:'+sys.argv[1])
except:
  pass
The results show up in /var/log/syslog

Code: Select all

Apr  9 09:36:43 pi Flash drive active
Apr  9 09:36:43 pi Argv[0]:/usr/local/bin/flash_drive
Apr  9 09:36:43 pi Argv[1]:sda'
You may want to try something like that to ensure your udev rules are catching the right event.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

texy
Forum Moderator
Forum Moderator
Posts: 5156
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: auto mounting usb stick and running a script

Mon Apr 11, 2016 8:51 am

Thanks Dougie. With usbmount installed, it seems the udev rules are activated prior to usbmount auto-mounting the stick :

Code: Select all

Apr 11 07:21:02 raspberrypi kernel: [  989.148971] usb-storage 1-1.2:1.0: USB Mass Storage device detected
Apr 11 07:21:02 raspberrypi kernel: [  989.149455] scsi host1: usb-storage 1-1.2:1.0
Apr 11 07:21:03 raspberrypi kernel: [  990.147618] scsi 1:0:0:0: Direct-Access     General  USB Flash Disk   1.0                               PQ: 0 ANSI: 2
Apr 11 07:21:03 raspberrypi kernel: [  990.149769] sd 1:0:0:0: [sda] 7831552 512-byte logical blocks: (4.01 GB/3                             .73 GiB)
Apr 11 07:21:03 raspberrypi kernel: [  990.150382] sd 1:0:0:0: [sda] Write Protect is off
Apr 11 07:21:03 raspberrypi kernel: [  990.150411] sd 1:0:0:0: [sda] Mode Sense: 03 00 00 00
Apr 11 07:21:03 raspberrypi kernel: [  990.150981] sd 1:0:0:0: [sda] No Caching mode page found
Apr 11 07:21:03 raspberrypi kernel: [  990.151008] sd 1:0:0:0: [sda] Assuming drive cache: write through
Apr 11 07:21:03 raspberrypi kernel: [  990.152226] sd 1:0:0:0: Attached scsi generic sg0 type 0
Apr 11 07:21:03 raspberrypi kernel: [  990.167846]  sda: sda1
Apr 11 07:21:03 raspberrypi kernel: [  990.170994] sd 1:0:0:0: [sda] Attached SCSI removable disk
Apr 11 07:21:03 raspberrypi Flash drive active
Apr 11 07:21:03 raspberrypi Argv[0]:/home/pi/flash_drive.py
Apr 11 07:21:03 raspberrypi Argv[1]:sda'
Apr 11 07:21:03 raspberrypi usbmount[1041]: /dev/sda does not contain a filesystem or disklabel
Apr 11 07:21:03 raspberrypi rsyslogd-2007: action 'action 17' suspended, next retry is Mon Apr 11 07:21:33 2016                              [try http://www.rsyslog.com/e/2007 ]
Apr 11 07:21:03 raspberrypi Flash drive active
Apr 11 07:21:03 raspberrypi Argv[0]:/home/pi/flash_drive.py
Apr 11 07:21:03 raspberrypi Argv[1]:sda1'
Apr 11 07:21:03 raspberrypi usbmount[1062]: executing command: mount -tvfat -osync,noexec,nodev,noatime,nodirati                             me /dev/sda1 /media/usb0
So I removed usbmount, and created a new automount.rules file with the following (copied via a google search!) :

Code: Select all

ACTION=="add",KERNEL=="sda*", RUN+="/usr/bin/pmount --sync --umask 000 %k"
ACTION=="remove", KERNEL=="sda*", RUN+="/usr/bin/pumount %k"
ACTION=="add",KERNEL=="sdb*", RUN+="/usr/bin/pmount --sync --umask 000 %k"
ACTION=="remove", KERNEL=="sdb*", RUN+="/usr/bin/pumount %k"
I also created another udev rule based on yours :

Code: Select all

KERNEL=="sd*", SUBSYSTEMS=="scsi", DRIVERS=="sd", SYMLINK+="flash",  RUN+="/usr/bin/python /home/pi/flash_drive.py $kernel'"
and the flash_drive.py looks like this :

Code: Select all

#!/usr/bin/python
import sys, os
import logging
import logging.handlers

my_logger = logging.getLogger('Flash_drive')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address = '/dev/log')
my_logger.addHandler(handler)

my_logger.debug('Flash drive active')
my_logger.debug('Argv[0]:'+sys.argv[0])
try:
  my_logger.debug('Argv[1]:'+sys.argv[1])
except:
  pass
print "I got here!"

os.system('sudo setsid /home/pi/usbcopy.sh')
(usbcopy.sh copies the files from the stick)
However this still didn't work. As the udev rules are carried out in numerical, then alphabetical file order, I renamed the automount.rules file to 10-local-rules.rules and viola - all working fine.

The only thing still outstanding on this is any form of console messaging - it would be useful to get some acknowledgement that valid files were found on the stick and copied to the system. Any form of print or echo is ignored. I understand that is the case with udev rules, but I was thinking that as the final usbcopy.sh script was actioned via the setsid command, the process would be separate and normal console IO would resume?
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: auto mounting usb stick and running a script

Mon Apr 11, 2016 9:14 am

Read the stackexchange link in the forum thread I posted above. There's example python code to double-fork & skirt around udev's seek'n'destroy mechanism.

Return to “Troubleshooting”