Intermittent failure executing on USB insert with auto-mount on Raspberry Pi

179 views Asked by At

I've set up a raspberry pi to execute a command if a USB stick is inserted, and the command calls an executable on the stick.

This works about 80% of the time, but intermittently fails - seemingly at random. Because of the unpredictability I assume this is a race condition, however I'm not too familiar with where the risks are as I've pieced together the approach from information online. Most of the information comes from here.

The USB stick is auto-mounted with the following entry in /etc/fstab. I'm aware of the risk of /dev/sda1 changing but that does not appear to be the problem here:

/dev/sda1 /media/usb vfat defaults,rw,nofail,user,umask=000 0 0

A service waits for the USB to mount with the following configuration

[Unit]
Description=USB Mount Trigger
Requires=media-usb.mount
After=media-usb.mount

[Service]
ExecStart=/script.sh

[Install]
WantedBy=media-usb.mount

media-usb.mount comes from systemctl list-units -t mount, and /script.sh calls the USB stick's executable.

In failure cases, where the USB's executable is not called, I see the following from systemctl status service_name:

Nov 15 22:49:14 raspberrypi systemd[1]: Dependency failed for USB Mount Trigger.
Nov 15 22:49:14 raspberrypi systemd[1]: service_name.service: Job service_name.service/start failed with result 'dependency'.

In these cases if I execute systemctl list-units -t mount I do not see media-usb.mount and my USB stick is not mounted to /media/usb.

I think that an error / race condition in service_name.service causing the USB mount to die, because (I believe) a successful mount is required to trigger the service. If the USB is never inserted, systemctl status service_name simply reports Active: inactive (dead), so something is triggering the service to try to execute.

0

There are 0 answers