Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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


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

# 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

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

Your Answer


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.