Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

i want to start a bash script in ubuntu after boot. But the script should run first after wlan0 device is connected to my wifi.

How can i achieve this?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

I'd start it at boot and let it wait until wlan0 is connected. You can check this via

#!/bin/bash

while true; do
    # testing...
    LC_ALL=C nmcli -t -f DEVICE,STATE dev | grep -q "^wlan0:connected$"
    if [ $? -eq 0 ]; then
        break
    else
        # not connected, sleeping for a second
        sleep 1
    fi
done

# now connected, run the script
share|improve this answer
    
great, works as a charme. thank you very much. note to other interested users: if your system language is not English you have to customize this part. "^wlan0:connected$" –  NES Dec 4 '10 at 11:06
    
Good catch! As mentioned in the man page of nmcli you can just start nmcli prepended by LC_ALL=C to avoid locale issues. –  htorque Dec 4 '10 at 11:17
add comment

In Ubuntu you can use upstart for this. Save the following as /etc/init/yourscript.conf

start on net-device-up wlan0

exec  your_script
share|improve this answer
    
I thought about this too, but does that actually wait for wlan0 to be connected or just for the interface being "ready"? –  htorque Dec 4 '10 at 11:54
    
Gave it a try and it runs 'your_script' once wlan0 is connected, so +1 for your answer. –  htorque Dec 4 '10 at 12:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.