Page 1 of 1

USB stick not always detected on power up

Posted: Tue Aug 16, 2016 9:27 am
by windy54
Hi,
Background.
Using latest raspbian and have created a Python data logging script that runs on power up using system to etc.
On power up it looks to see if a USB drive is mounted by checking to see if /media/PI has a sub folder. If so, it opens a file on it and writes to it. If not, it writes to a folder on the SD card.
Maybe not the most elegant approach but it seemed to work .

However, I have noticed that sometimes when the USB drive is inserted and the PI powers up it is not detected.
If I use "sudo systemctl stop .." to stop the script and then restart it the drive is always detected.

Could there be some timing issue on power up, do I need to wait for the drive to be mounted (somehow), I did think of just adding a time.sleep(5) in the script.

Cheers

Steve

Re: USB stick not always detected on power up

Posted: Tue Aug 16, 2016 11:39 am
by epoch1970
I think your script is either run before the key is detected, or before systemd has finished fast-checking the filesystem (so the partition is not yet mounted)

A sleep or "sleep+some number of retries" in your script should resolve the issue, it is a bit brute-force but would probably work.
The elegant way of doing it, IMHO, would be to write an udev rule that reacts to the appearance of the USB key or of the partition and calls your script (possibly with a parameter "key has arrived/is gone")
In the forum I've seen some interesting applications of udev rules.

Re: USB stick not always detected on power up

Posted: Wed Aug 17, 2016 7:19 am
by windy54
Thanks for the response , I have gone for the delay and that has fixed it for the moment.

I will have a look at user rules out of interest

Steve