如何解决libqmi与networkd-dispatcher初始化LTE模块时脚本重复执行的问题
Let’s break down why your script keeps running over and over, and walk through concrete fixes to stabilize your system:
The Root Cause
Your script lives in /etc/networkd-dispatcher/degraded.d/—this directory triggers scripts every time the network enters a degraded state. The problem? Your script’s actions (like toggling the interface up/down or modifying QMI settings) are probably causing the network state to flip between degraded and other states, creating an endless loop. Adding sleep 10 doesn’t fix this because it doesn’t address why the state is changing repeatedly.
1. Move the Script to a Better Directory
Instead of reacting to degraded states, run the script only when the WWAN interface first comes online. Use one of these networkd-dispatcher directories instead:
up.d: Triggers when the interface transitions to the "up" statepost-up.d: Triggers after the interface is fully up and configured
This way, your script runs once when the interface starts, not every time the network hits a blip.
2. Add a Lock File to Block Re-Runs
Even with the right directory, a lock file ensures the script never runs multiple times at once or in quick succession. Update your script with this logic:
#!/bin/bash # Create a lock file to prevent duplicate runs LOCK_FILE="/var/run/wwan_init.lock" if [ -f "$LOCK_FILE" ]; then echo "WWAN setup already in progress or done—exiting." exit 0 fi touch "$LOCK_FILE" trap 'rm -f "$LOCK_FILE"' EXIT # Clean up lock when the script finishes interface="${IFACE}" if [[ "$interface" == *"wwan"* ]]; then device="/dev/cdc-wdm${interface: -1}" is_wwan="y" fi if [[ "$is_wwan" == "y" ]]; then # Only make changes if they're needed # Check if raw_ip is already enabled if [ "$(cat "/sys/class/net/${interface}/qmi/raw_ip")" != "Y" ]; then echo "Y" > "/sys/class/net/${interface}/qmi/raw_ip" fi # Check if autoconnect is already enabled if ! qmicli -p -d "${device}" --wds-get-autoconnect-settings | grep -q "enabled"; then ip link set dev "${interface}" down qmi-network "${device}" start || true qmicli -p -d "${device}" --wds-set-autoconnect-settings=enabled || true ip link set dev "${interface}" up fi fi
3. Cut Out Unnecessary Interface Toggles
Your original script brings the interface down/up every time it runs, which is a big trigger for repeated state changes. By adding checks (like verifying raw_ip and autoconnect status first), you only modify the interface when it’s actually needed—no more unnecessary state flips.
4. Check for Conflicting Scripts
Make sure no other networkd-dispatcher scripts are messing with your WWAN interface. Run this to list all active scripts:
ls -la /etc/networkd-dispatcher/
Look for any scripts in other directories (like down.d or dormant.d) that might be modifying the WWAN interface and causing state loops.
内容的提问来源于stack exchange,提问作者Florian Berndl

