TransWikia.com

Successfully start systemd service only if eth0 is not connected to the internet after boot

Unix & Linux Asked by ustayready on November 9, 2021

Problem:

I am attempting to start a systemd service which enables a connection to an LTE/GSM provider by running a script but I only want the service to successfully start when eth0 is in the disconnected state. Additionally, I am only concerned with triggering the service after boot and not if eth0 goes into a disconnected state at a later time if it’s going to be more effort.

Attempted:

What I have so far is a script located at /etc/network/if-up.d/lte-check which writes a temp file to /var/run/inet-success. I also have a systemd service that checks to make sure the file does not exist before starting using ExecStartPre.

Results:

When eth0 is plugged in at boot (with an internet connection), the inet-success file is written to the volatile storage and the LTE service does not run. This is the expected behavior.

When eth0 is not plugged in at boot, the inet-success file is not written to the volatile storage and the LTE service does not run. This is NOT the expected behavior.

Additionally, after booting with eth0 plugged in (with an internet connection), manually deleting /var/run/inet-success triggers the systemd service to start correctly.

Environment:

Raspberry Pi4
Operating System: Raspbian GNU/Linux 10 (buster)
Kernel: Linux 4.19.118-v7l+
Architecture: arm

/etc/network/if-up.d/lte-check

#!/bin/sh
FLAGFILE=/var/run/inet-success

case "$IFACE" in
        "--all") : ;;
        *) exit 0;;
esac
if [ -e $FLAGFILE ]; then
    exit 0
else
    /usr/bin/touch $FLAGFILE
fi

/etc/systemd/system/lte.service

[Unit]
Description=LTE Service
After=network.target
StartLimitIntervalSec=0

[Service]
ExecStartPre=/usr/bin/test ! -f /var/run/inet-success
ExecStart=/opt/lte/files/quectel-CM/quectel-CM -s wireless.twilio.com
RestartSec=1m
Restart=on-failure
User=root
Type=simple

[Install]
WantedBy=multi-user.target

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP