[Solved] Pause script until Wifi/network connects

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
xircon

[Solved] Pause script until Wifi/network connects

Post by xircon »

This requires a little explanation. I run LMDE 64bit, with LightDM as my login manager and Compiz as my window manager. With this I run a hand picked selection of programs. I am having problems with programs launching, that require WIFI, before WICD has connected. I found this on the Ubuntu forum:

Code: Select all

until wget -q -O - http://www.google.com | grep Lucky > /dev/null; do
	sleep 10
done
This works, but am worried that if I do not get a connection it is just going to cycle forever and not complete the script. I have re-jigged my script to make it less of a problem, but could do with a "get out clause" after a length of time.

My full script now looks like this:

Code: Select all

#!/bin/bash

#dbus
if which dbus-launch >/dev/null && test -z "$DBUS_SESSION_BUS_ADDRESS"; then
eval `dbus-launch --sh-syntax --exit-with-session`
fi

sudo /etc/init.d/wicd start

sudo umount ~/.gvfs &
killall knotify4 &

#############
## Tidy up ##
#############
rm -rf /home/molly/Downloads/*.torrent &

## Compiz, Emerald & Rox
/usr/bin/gnome-keyring-daemon &
compiz --replace ccp &
gnome-settings-daemon &
emerald & 
rox --pinboard=default &
avant-window-navigator & 

## Appearance/Power/Volume 
gnome-power-manager &
gnome-volume-control-applet &

#################################
## Utils that do not need wifi ##
#################################
yakuake &
parcellite &
autokey-gtk &
setxkbmap -option terminate:ctrl_alt_bksp &
/usr/bin/bluetooth-applet &
shutter --min_at_startup &
tomboy &
pino &
indicator-keylock &

##Screensaver
xscreensaver &

#Sound
# Add your user to audio group

## Mint proggies
/usr/bin/mintupdate-launcher &
/usr/lib/linuxmint/mintUpload/launch-file-uploader.py &

########################
## Wireless / Network ##
########################
wicd-client &

#Wait till WIFI connects
until wget -q -O - http://www.google.com | grep Lucky > /dev/null; do
	sleep 10
done

#####################################
## Utils that require wifi to run ###
#####################################

## Screenlets
#python -u /home/molly/.screenlets/FolderView/FolderViewScreenlet.py &
#python -u /usr/share/screenlets/screenlets-pack-all/Gmail/GmailScreenlet.py &
python -u /usr/share/screenlets/screenlets-pack-all/ClearWeather/ClearWeatherScreenlet.py &
/home/molly/scripts/checkgmail.pl &

## Online Storage
dropbox start -i &
SpiderOak &
u1sdtool -s &
ubuntuone-launch &
/usr/bin/minus & 

#Truecrypt
#(sleep 25 && truecrypt) &

fusion-icon
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Roken
Level 5
Level 5
Posts: 738
Joined: Fri Nov 19, 2010 4:55 pm
Location: Newport, S Wales

Re: Pause script until Wifi/network connects

Post by Roken »

I haven't tested this, but try:

Code: Select all

lcount=0
while ["$lcount" != "10"]
if [ wget -q -O - http://www.google.com | grep Lucky > /dev/null]; then
   sleep 10
   $lcount=$(($lcount+1))
fi
This should stop the loop after 10 iterations.
Kernel Linux Tex 5.12.14-zen1-1-zen, XFCE
Arch
Dual GTX1070 8Gb
AMD Ryzen 1800X
32Gb RAM
xircon

Re: Pause script until Wifi/network connects

Post by xircon »

Cheers, just couldn't think of a way to do it, will test tomorrow and report back as I will not be popular with my missus if I turn off the router now :)
xircon

Re: Pause script until Wifi/network connects

Post by xircon »

Roken could not get your script to work, but it has pointed me in the right direction:

Code: Select all

#!/bin/bash

i="0"

while [ $i -lt 10 ]
do
until wget -q -O - http://www.google.com | grep Lucky > /dev/null; do
        i=$[$i+1]
	sleep 1  #Reduce to one to speed up debug
	if [ "$i" -gt "9" ]; then
	   break       	   
        fi
        done
i=$[$i+1]
done
Still in the process of testing, the one thing I don't understand is this line if [ "$i" -gt "9" ]; then if I try if [ "$i" = "9" ]; then the script fails. I thought:

Code: Select all

#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
   echo expression evaluated as true
       else
   echo expression evaluated as false
fi
Taken from http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html would work.
Last edited by xircon on Wed Oct 12, 2011 7:35 am, edited 1 time in total.
xircon

Re: Pause script until Wifi/network connects

Post by xircon »

Right, been messing with this all morning and have a much more elegant solution using grep -c to return line number, which I find easier to test for:

Code: Select all

#!/bin/bash
i="0"

while [ $i -lt 10 ]; do 
a=$(wget -q -O - http://www.google.com | grep -c "Lucky")
echo $a
if [ "$a" -gt "0" ]; then
    i="10"
   else
    sleep 10
    i=$[$i+1]
fi
done
User avatar
Roken
Level 5
Level 5
Posts: 738
Joined: Fri Nov 19, 2010 4:55 pm
Location: Newport, S Wales

Re: Pause script until Wifi/network connects

Post by Roken »

As I said, I hadn't tested, and TBH I find "If" tests in bash to be temperamental unless you get them exactly right, so that's probably the problem in my original suggestion.

Glad it got you on the right track though :D
Kernel Linux Tex 5.12.14-zen1-1-zen, XFCE
Arch
Dual GTX1070 8Gb
AMD Ryzen 1800X
32Gb RAM
xircon

Re: Pause script until Wifi/network connects

Post by xircon »

Will mark it solved. Still got to try it with the router turned off, my other half runs a large forum, so got to wait until she has finished.

Cheers

Steve
Locked

Return to “Scripts & Bash”