Hello,
I have a problem where some commands no longer work when running them from the Startup Script.
The commands giving issues are:
Ubus
timeout
/etc/init.d/rms_mqtt restart
They code works perfectly when running them from CLI.
Here is my code:
#!/bin/ash
sleep 30
LOGFILE=“/tmp/startup_debug.log”
echo “===== Script Start: $(date) =====” >> “$LOGFILE” 2>&1
echo “Restarting MQTT service…” >> “$LOGFILE” 2>&1
if [ -x /etc/init.d/rms_mqtt ]; then
/etc/init.d/rms_mqtt restart >> “$LOGFILE” 2>&1
else
echo “MQTT init script not found” >> “$LOGFILE”
fi
serial_Nr=$(cat /sys/mnf_info/serial 2>> “$LOGFILE”)
echo “Serial: $serial_Nr” >> “$LOGFILE” 2>&1
BROKER=Broker_Ip
PORT=Port
TOPIC=“/${serial_Nr}_WR”
RESPONSE_TOPIC=“/${serial_Nr}_WR/response”
USERNAME=“Username”
PASSWORD=“Password”
QOS=0
RETRY_INTERVAL=10
MAX_RETRIES=10
echo “TOPIC: $TOPIC” >> “$LOGFILE” 2>&1
echo “RESPONSE_TOPIC: $RESPONSE_TOPIC” >> “$LOGFILE” 2>&1
check_internet() {
ping -c 1 -W 1 8.8.8.8 >> “$LOGFILE” 2>&1 || curl -s --head --max-time 2 https://www.google.com >> “$LOGFILE” 2>&1
return $?
}
ensure_package_installed() {
package_name=“$1”
max_retries=5
retry_interval=30
retry_count=0
while true; do
if opkg list-installed | grep -q "^$package_name " >> "$LOGFILE" 2>&1; then
echo "Package '$package_name' is already installed." >> "$LOGFILE" 2>&1
return 0
fi
echo "Checking internet connectivity..." >> "$LOGFILE" 2>&1
while ! check_internet; do
retry_count=$((retry_count + 1))
if [ "$retry_count" -ge "$max_retries" ]; then
echo "No internet connectivity after $max_retries attempts. Exiting for now. Retry later." >> "$LOGFILE" 2>&1
exit 1
fi
echo "No internet connection. Retrying in $retry_interval seconds... ($retry_count/$max_retries)" >> "$LOGFILE" 2>&1
sleep "$retry_interval"
done
opkg -e /etc/opkg/openwrt/distfeeds.conf update >> "$LOGFILE" 2>&1
echo "Attempting to install package '$package_name'..." >> "$LOGFILE" 2>&1
if opkg -e /etc/opkg/openwrt/distfeeds.conf install "$package_name" >> "$LOGFILE" 2>&1; then
echo "Package '$package_name' installed successfully." >> "$LOGFILE" 2>&1
return 0
else
echo "Failed to install package '$package_name'. Retrying in $retry_interval seconds..." >> "$LOGFILE" 2>&1
sleep "$retry_interval"
fi
done
}
required_packages=“curl coreutils-timeout mosquitto-client-ssl”
for package in $required_packages; do
ensure_package_installed “$package”
done
attempts=0
while ! ubus list | grep -q modbus_client && [ $attempts -lt 10 ]; do
echo “Waiting for modbus_client UBUS service…” >> “$LOGFILE”
sleep 5
attempts=$((attempts+1))
done
for i in $(seq 1 $MAX_RETRIES); do
echo “Attempting to connect to MQTT broker (Attempt $i)…” >> “$LOGFILE” 2>&1
mosquitto_pub -h $BROKER -p $PORT -t "/test/connection" -u $USERNAME -P $PASSWORD -m "test" -q $QOS >> "$LOGFILE" 2>&1
if [ $? -eq 0 ]; then
echo "Connected to MQTT broker successfully." >> "$LOGFILE" 2>&1
mosquitto_sub -h $BROKER -p $PORT -t $TOPIC -u $USERNAME -P $PASSWORD -q $QOS | while IFS=',' read -r ID FUNCTION FIRST_REG VALUE DATA_TYPE IP_ADDRESS
do
echo "Received command: ID=$ID, FUNCTION=$FUNCTION, FIRST_REG=$FIRST_REG, VALUE=$VALUE, DATA_TYPE=$DATA_TYPE, IP_ADDRESS=$IP_ADDRESS" >> "$LOGFILE" 2>&1
if ubus list | grep -q modbus_client; then
RESPONSE=$(ubus call modbus_client.rpc tcp.test "{
\"id\": $ID,
\"timeout\": 5,
\"function\": $FUNCTION,
\"first_reg\": $FIRST_REG,
\"reg_count\": \"$VALUE\",
\"data_type\": \"$DATA_TYPE\",
\"no_brackets\": 1,
\"ip\": \"$IP_ADDRESS\",
\"port\": \"502\",
\"delay\": 0
}" 2>> "$LOGFILE")
if [ $? -eq 0 ]; then
echo "Command executed successfully. Response: $RESPONSE" >> "$LOGFILE" 2>&1
timeout 4 mosquitto_pub -h $BROKER -p $PORT -t "$RESPONSE_TOPIC" -u $USERNAME -P $PASSWORD -m "$RESPONSE" -q $QOS >> "$LOGFILE" 2>&1
else
echo "Failed to execute write command." >> "$LOGFILE" 2>&1
echo "Response was: $RESPONSE" >> "$LOGFILE" 2>&1
fi
else
echo "modbus_client not available after waiting. Skipping." >> "$LOGFILE"
fi
done
break
else
echo "Failed to connect to MQTT broker. Retrying in $RETRY_INTERVAL seconds..." >> "$LOGFILE" 2>&1
sleep $RETRY_INTERVAL
fi
done
if [ $? -ne 0 ]; then
echo “Failed to connect to MQTT broker after $MAX_RETRIES attempts.” >> “$LOGFILE” 2>&1
exit 1
fi
echo “===== Script End: $(date) =====” >> “$LOGFILE” 2>&1
And the log returned:
===== Script Start: Tue May 13 11:20:16 UTC 2025 =====
Restarting MQTT service…
Command failed: Not found
Serial: Serial Nr
TOPIC: /Serial Nr_WR
RESPONSE_TOPIC: /Serial Nr_WR/response
Package ‘curl’ is already installed.
Package ‘coreutils-timeout’ is already installed.
Package ‘mosquitto-client-ssl’ is already installed.
Attempting to connect to MQTT broker (Attempt 1)…
Connected to MQTT broker successfully.
Received command: ID=1, FUNCTION=3, FIRST_REG=401, VALUE=1, DATA_TYPE=16bit_uint_hi_first, IP_ADDRESS=192.168.0.8
Command failed: Not found
Failed to execute write command.
Response was: