Troubleshooting Delayed Execution and Prompt Exit Issue in Bash Script

Hello,

I've created a bash script that allows me to set up port mirroring on my router. I also have a script that I place internally on the router to retrieve the IP address on port 4, but that's not the issue here.

In fact, the purpose of this script is to run in the background indefinitely until I stop it. When I'm connected via SSH to my router and I type this command:

bash

sh /etc/config/find_ip_lan4.sh &

The script works fine.

However, when I use my bash script on my PC and the program launches, the previous command is not taken into account, and I think it's because of the 5-second delay and the prompt that exits before giving the confirmation message:

bash

root@OpenWrt:/etc/config# sh /etc/config/find_ip_lan4.sh &
root@OpenWrt:/etc/config#
[1]+ Done sh /etc/config/find_ip_lan4.sh
root@OpenWrt:/etc/config#

Here's my script:

bash

# Function to configure the hub and port mirroring
configure_hub() {
    
    # Check that the IP address is in the correct format
    # /etc/init.d/port_mirroring stop
    # Update port mirroring configuration with uci
    uci set port-mirroring.@port-mirroring[0].source_ports=lan2
    uci set port-mirroring.@port-mirroring[0].promiscuous=1
    uci set port-mirroring.@port-mirroring[0].target="$ip_address"
    uci set port-mirroring.@port-mirroring[0].protocol=TZSP
        
    # Save the configuration
    uci commit port-mirroring
    echo "Port mirroring configuration updated successfully."
    
    # Create the directory if it doesn't exist
    mkdir -p /etc/config
    # Write the script to the file
    echo '#!/bin/sh

    SLEEP=5
    PHRASE="Network device '\''lan4'\'' link is up"
    CONFIG_FILE="/etc/config/port-mirroring"

    extract_ip() {
        ip=$(logread | grep -A 10 "Network device '\''lan4'\'' link is up" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | tail -n 1)

        if [ -n "$ip" ]; then
            uci set port-mirroring.@port-mirroring[0].target="$ip"
            uci commit port-mirroring
        else
            exit 0
        fi
    }

    while sleep $SLEEP; do
    extract_ip
    done' > /etc/config/find_ip_lan4.sh

    chmod +x /etc/config/find_ip_lan4.sh
    
    /etc/init.d/port_mirroring stop
    /etc/init.d/port_mirroring start
}

init() {
    sh /etc/config/find_ip_lan4.sh &
    /etc/init.d/port_mirroring restart
}

ssh root@$router_ip_envoie  "$(declare -f configure_hub); configure_hub $router_ip_envoie "
ssh root@$router_ip_envoie  "$(declare -f init); init $router_ip_envoie "

Thank you for your help!