Skip to content

Instantly share code, notes, and snippets.

@libbymiller
Last active August 15, 2020 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save libbymiller/e8fe6821e122e0a0ac921c8e557320a9 to your computer and use it in GitHub Desktop.
Save libbymiller/e8fe6821e122e0a0ac921c8e557320a9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# export DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket
# ^^^ this can cause barfing and isn't needed
# Choose a condition for running WiFi Connect according to your use case:
# 1. Is there a default gateway?
# ip route | grep default
# 2. Is there Internet connectivity?
# nmcli -t g | grep full
# 3. Is there Internet connectivity via a google ping?
# wget --spider http://google.com 2>&1
# 4. Is there an active WiFi connection?
sleep 30s
iwgetid -r
if [ $? -eq 0 ]; then
printf 'Skipping WiFi Connect\n'
else
printf 'Starting WiFi Connect\n'
wifi-connect -o 7777
fi
# Start your application here.
@johnwason
Copy link

I tried using the instructions in your blog post for Ubuntu, and it required a few tweaks:

  • wireless-tools is not installed by default, so I had to run sudo apt install wireless-tools
  • The wifi-connect script was being run before the system was able to form a wifi connection, so it was being reset every boot. Adding ExecStartPre=/bin/sleep 10 to the service configuration fixed this problem.
  • The path to file should use /home/ubuntu instead of /home/pi when using Ubuntu instead of Raspian

Otherwise your blog post was very helpful!

@libbymiller
Copy link
Author

Thank you! I've updated it to add in a delay (and also I'm running it on a different port).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment