I'm new to the whole Raspberry Pi and Bash scripting thing, so bear with me.

I run raspberry autopilot on a remotely controlled vehicle thru a 3G/4G connection. Raspberry Pi is installed onboard the vehicle. It connects to an OpenVPN server, and sends UDP telemetry to a machine on the VPN network.

The 3G/4G coverage is not stable so I need to keep an eye on it. I have crontab script which runs every minute:

2 Answers
2

It is difficult to give some specific hints because there is no information about the network setup. But usually you can check if an interface is online. There is a tool from systemd that can check it:

rpi ~$ /lib/systemd/systemd-networkd-wait-online --help
systemd-networkd-wait-online [OPTIONS...]
Block until network is configured.
-h --help Show this help
--version Print version string
-q --quiet Do not show status information
-i --interface=INTERFACE Block until at least these interfaces have appeared
--ignore=INTERFACE Don't take these interfaces into account
--timeout=SECS Maximum time to wait for network connectivity

You can look with ip addr what interfaces available and what interface has to be online after resetting the USB modem connection. I don't know if the tool detects the interface for the 4G connection but it is worth a try. Just replace the sleep 45 statement for example with:

/lib/systemd/systemd-networkd-wait-online --interface=enx00a0c60429a0 --timeout=120 --quiet times out with error while interface is up. Is that a correct behavior?
– Vladimir PJan 1 at 19:09

btw, this interface state is unknown when checked with ip addr
– Vladimir PJan 1 at 19:16

@user95138 Please edit your question and add the output from ip addr and from ip route when a 4G connection is established. When testing you can also omit option --quiet to see messages.
– IngoJan 1 at 19:28

Fixed the question. The output without quiet is just “ignoring: lo” which probably means it ignores loopback interface.
– Vladimir PJan 1 at 19:41

@user95138 Try to prefix the command with sudo. I have updated my answer with it. I've just looked at the tool and it seems it only checks for gained carrier. You can find this status in journalctl -b (or even not). I'm looking at it, just a moment ...
– IngoJan 1 at 20:23