Troubleshooting Delayed Execution and Prompt Exit Issue in Bash Script


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:


sh /etc/config/ &

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:


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

Here's my script:


# 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

    PHRASE="Network device '\''lan4'\'' link is up"

    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
            exit 0

    while sleep $SLEEP; do
    done' > /etc/config/

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

init() {
    sh /etc/config/ &
    /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!