[Solved] Pause script until Wifi/network connects

Forum rules
Before you post please read how to get help

[Solved] Pause script until Wifi/network connects

Postby xircon on Tue Oct 11, 2011 5:21 pm

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 xircon on Wed Oct 12, 2011 8:18 am, edited 1 time in total.
Dell Inspiron N5010 / Core I5 / ATI Mobility Radeon HD5400, LMDE Tracking Sid & Cinnamon Desktop
"Ubuntu is an African word meaning 'I can't configure Debian'"
User avatar
xircon
Level 5
Level 5
 
Posts: 775
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

Linux Mint is funded by ads and donations.
 

Re: Pause script until Wifi/network connects

Postby Roken on Tue Oct 11, 2011 7:07 pm

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 3.2.2-1 (64 bit) xfce , burg
Arch
ATI Radeon HD 4670, 1GB
OSS4
AMD Phenom II X4 (965BE) @ 3.6 Ghz (Overclocked)
8Gb RAM
Image
User avatar
Roken
Level 5
Level 5
 
Posts: 747
Joined: Fri Nov 19, 2010 4:55 pm
Location: Oldham, UK

Re: Pause script until Wifi/network connects

Postby xircon on Tue Oct 11, 2011 7:17 pm

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 :)
Dell Inspiron N5010 / Core I5 / ATI Mobility Radeon HD5400, LMDE Tracking Sid & Cinnamon Desktop
"Ubuntu is an African word meaning 'I can't configure Debian'"
User avatar
xircon
Level 5
Level 5
 
Posts: 775
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

Re: Pause script until Wifi/network connects

Postby xircon on Wed Oct 12, 2011 5:45 am

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.
Dell Inspiron N5010 / Core I5 / ATI Mobility Radeon HD5400, LMDE Tracking Sid & Cinnamon Desktop
"Ubuntu is an African word meaning 'I can't configure Debian'"
User avatar
xircon
Level 5
Level 5
 
Posts: 775
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

Re: Pause script until Wifi/network connects

Postby xircon on Wed Oct 12, 2011 7:25 am

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
Dell Inspiron N5010 / Core I5 / ATI Mobility Radeon HD5400, LMDE Tracking Sid & Cinnamon Desktop
"Ubuntu is an African word meaning 'I can't configure Debian'"
User avatar
xircon
Level 5
Level 5
 
Posts: 775
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

Re: Pause script until Wifi/network connects

Postby Roken on Wed Oct 12, 2011 8:13 am

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 3.2.2-1 (64 bit) xfce , burg
Arch
ATI Radeon HD 4670, 1GB
OSS4
AMD Phenom II X4 (965BE) @ 3.6 Ghz (Overclocked)
8Gb RAM
Image
User avatar
Roken
Level 5
Level 5
 
Posts: 747
Joined: Fri Nov 19, 2010 4:55 pm
Location: Oldham, UK

Re: Pause script until Wifi/network connects

Postby xircon on Wed Oct 12, 2011 8:17 am

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
Dell Inspiron N5010 / Core I5 / ATI Mobility Radeon HD5400, LMDE Tracking Sid & Cinnamon Desktop
"Ubuntu is an African word meaning 'I can't configure Debian'"
User avatar
xircon
Level 5
Level 5
 
Posts: 775
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 4 guests