Search code examples
linuxbashgoogle-chromeubuntuvnc

BASH - how to execute few applications when the network interface is up and accessable to internet?


I have following script which is running via /etc/init.d/service on system boot. But on system boot sometimes the DHCP ip is invalid from the router as a result it shows empty page or invalid page. How do i execute this following chromium-browser if it can really ping 8.8.8.8 or similar?

So that it does not show invalid page and does not require human involved with keyboard to refresh the page or restart the script manually?

#!/bin/bash
export DISPLAY=:0.0
pgrep -f chromium-browser | xargs kill
pgrep -f x11vnc | xargs kill
sudo -u user1 chromium-browser "https://stackoverflow.com/questions" &
sudo -u user1 x11vnc -forever -passwd 1234 &

Solution

  • You can add a timer in the script itself:

    while ! ping -c 1 8.8.8.8 | grep '1 received'
    do
        :
    done
    

    If you want to ensure it doesn't run forever:

    timeout=600 # seconds
    start_time="$(date +%s)"
    end_time="$(($start_time + $timeout))"
    while ...
        if [ "$(date +%s)" -gt "$end_time" ]
        then
            echo "$0 timed out" >&2
            exit 1
        fi
        ...
    done