I keep on getting error in rc.local

1.8k Views Asked by At

I have the following script added to my rc.local file

#wait until the internet connection is ready
wget -q --tries=10 --timeout=20 -O - http://google.com > /dev/null
while [ $? -ne 0 ]; do
        printf "Waiting for internet connection"
        sleep  10;
        wget -q --tries=10 --timeout=20 -O - http://google.com > /dev/null;

done

If I am connected to internet everything just works fine but if I am offline I get error

[FAIL] startpar: service(s) returned failure: rc.local ... failed!

What is wrong here and how could I fix it, I want rc.local to wait for internet connection before it continues to another script

1

There are 1 best solutions below

0
On BEST ANSWER

As it appears to have solved your problem, this is how I would suggest writing your code:

while ! wget -q --tries=10 --timeout=20 -O - http://google.com > /dev/null; do
    echo 'Waiting for internet connection'
    sleep 10
done

Remember that [ is not syntax, it is a command whose exit status determines whether your while loop continues or not. You should just test the exit status of the command you are trying to run directly.

As for why this has solved your problem, to be honest I'm not sure. The error message that you have shown isn't really specific enough to indicate where the problem lies and I can't see anything actually wrong with what you did before. Perhaps you should try adding set -xv to your rc.local file to enable debugging.