I use an USB LTE modem for connecting my Ubuntu 18.04 server to LTE metwork. The modem is 12d1:1506 Huawei Technologies Co., Ltd. Modem/Networkcard. I wrote a systemd script which starts the connection at boot:
[Unit]
Description=Swan LTE connection
Requires=network-online.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=/root/swan_connect.sh
[Install]
WantedBy=multi-user.target
swan_connect.sh contains:
#!/bin/bash
/bin/echo -e "AT^NDISDUP=1,1,\"internet2\"\r" > /dev/ttyUSB0
/bin/sleep 2
/usr/sbin/netplan apply
It works fine but the problem is LTE connection is dropped every 48 hours (or in case of outage) and I need to re-connect and get new IP. I do it currently by a crontab script each minute - if I can't ping an IP, then it will reconnect by these commands:
/usr/sbin/netplan apply
/bin/echo -e "AT^NDISDUP=1,0,\"internet2\"\r" > /dev/ttyUSB0
/bin/sleep 2
/bin/echo -e "AT^NDISDUP=1,1,\"internet2\"\r" > /dev/ttyUSB0
/bin/sleep 2
/usr/sbin/netplan apply
Again - it works fine but I was just thinking whether there would not be a systemd version of this re/connecting. Is it possible to make a systemd script in a way systemd would re-connect the LTE connection if dropped?
systemd
offers a number of different ways to activate services, including path-based activation to start a service if a file state changes.There is no built-in facility to react to the network connection being down.
systemd
does include systemd timers as a replacement for Cron, but the end result would the same design: Perform a check on an interval.