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?


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
  • 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
  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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