Sample Header Ad - 728x90

How to Stop Script from Overwriting Symlink to a File

1 vote
1 answer
38 views
long time reader, but first questioner here. (Feedback on my question is welcome) I'm using a Raspberry Pi CM4 with a custom PCB. The board includes a USB Hub to allow for 2-3 peripherals. Hardware works great, no issues there. Two of the USB connections are permanently integrated into the board and are randomly assigned device names each startup. (ie. the 4G modem could be assign ttyUSB0-3 or ttyUSB1-4). The 4G Module can also be swapped out to a few different brands. There are services that I run to check on the 4G modem after starting up (to get serial numbers and manufacturers etc.) and periodically, to get things such us signal strength. I've done this using a shell script that sends AT-Commands to the module via socat. e.g: echo "AT+GSN" | socat - ${MODEM_LOCATION} Where MODEM_LOCATION points towards the device path. I had been manually checking the device path and starting the services but wanted to automate that process. Following some advice I read online, I developed some udev rules to create a symlink to the Modem, so it can autonomously start the scripts. SUBSYSTEMS=="usb", ENV{ID_USB_INTERFACE_NUM}="$attr{bInterfaceNumber}" SUBSYSTEM=="tty", ATTRS{idProduct}=="0125", ATTRS{idVendor}=="2c7c", MODE="0777", ENV{ID_USB_INTERFACE_NUM}=="00", SYMLINK+="ttyUSB_EC25_1" SUBSYSTEMS=="usb", ENV{ID_USB_INTERFACE_NUM}="$attr{bInterfaceNumber}" SUBSYSTEM=="tty", ATTRS{idProduct}=="0125", ATTRS{idVendor}=="2c7c", MODE="0777", ENV{ID_USB_INTERFACE_NUM}=="01", SYMLINK+="ttyUSB_EC25_2" SUBSYSTEMS=="usb", ENV{ID_USB_INTERFACE_NUM}="$attr{bInterfaceNumber}" SUBSYSTEM=="tty", ATTRS{idProduct}=="0125", ATTRS{idVendor}=="2c7c", MODE="0777", ENV{ID_USB_INTERFACE_NUM}=="02", SYMLINK+="ttyUSB_EC25_3" SUBSYSTEMS=="usb", ENV{ID_USB_INTERFACE_NUM}="$attr{bInterfaceNumber}" SUBSYSTEM=="tty", ATTRS{idProduct}=="0125", ATTRS{idVendor}=="2c7c", MODE="0777", ENV{ID_USB_INTERFACE_NUM}=="03", SYMLINK+="ttyUSB_EC25_4" At first it worked a charm while I was manually running the scripts, but when I started getting the service to run it, it began 'breaking' the symlink. What I mean by breaking it, is that it's no longer a symlink path. Instead it becomes a file that's storing text - the commands and responses from the AT-Commands. In the screenshot below you can see it's changed the file type. Screenshot of the results from ls /dev/ttyUSB* Additionally, I can run nano /dev/ttyUSB_EC25_4 and it opens an unwritable file full of the communications with the module: Screenshot showing the file contents I've tried to debug it from the point of view the socat was the issues - no luck. I've tried to debug the script I was running. I've tried to run the scripts manually now as well but it's still happening. (Unsure why it seemed to be working originally) I've spent about 3 weeks on this issue now and would very much appreciate any advice, solutions, recommendations etc. If I remove the file and restart the rules it re-creates a symlink correctly, but after running the scripts again, even if it works the first or second time, by the third time it's been changed back to some kind of file.
Asked by Oliver Munro (11 rep)
Aug 22, 2024, 01:07 AM
Last activity: Sep 2, 2024, 03:41 AM