0

First some background: I'm using the LaunchD feature in Mac OSX to periodically launch an application I'll call "AppX". Optimally I like to run this application nearly 24/7. But due to issues with memory leakage (that is my best guess), AppX closes periodically. To solve this, I've created and loaded a simple plist file to launch the application every 6 hours. This itself works perfectly and minimizes application downtime. However, AppX itself can be a drain on my battery, and I'd prefer it only launch when I'm at home, connected to my wifi network.

Please be aware that while I have some experience with C++ and Java, I know very little in the way of Unix.

My question: I'd like to use an if statement to check whether the network I'm connected to is my home wifi network. Being the case that it is, the system will execute the command:

open -a AppX

So... How would I implement an if statement to accomplish this? Any help is appreciated.

1 Answers1

0

There's an older SO question that gives part of the answer:

Get wireless SSID through shell script on Mac OS X

As for the if statement, the following should work:

homenet = "MyHomeNetwork"
netname = /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'
if [ "$netname" -eq "$homenet" ]
then
    # Do fancy service startup here
else
    # This is not my home network
fi
Community
  • 1
  • 1
roelofs
  • 2,132
  • 20
  • 25
  • Awesome, Thanks. This worked well after a bit of tweaking (took me hours but a great learning experience). I got it to work using the following code: `homenet="HOMENET"; netname=``/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}’``; if [ $netname = $homenet ]; then open -a AppX; fi` Easier to read if semicolons are replaced with newlines, but the code had to be all one line for the purposes of launchd. Also, double ticks (``) are actually single ticks (`). Formatting... – user3923537 Aug 10 '14 at 06:19