Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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

Is there a way to run a script every time a network interface finishes coming up? Or, is there an equivilent answer to this Debian question on OSX?

share|improve this question
up vote 0 down vote accepted
+100

I haven't tried this solution my self - yet, but found this thread: http://superuser.com/questions/201478/run-script-in-os-x-10-6-on-network-connection-like-etc-network-if-up-d/261004#261004

You should consider using crankd, which precisely allows you to run scripts in response to many system events such as network changes, filesystem activity, application launching, etc.

As I couldn't find any sensible documentation, I also wrote a small blog post on getting started using crankd.

share|improve this answer
    
I checked stackoverflow and the googles, but didn't happen across superuser. Regardless, this is it! Thanks. – Matt Dec 2 '11 at 2:22
    
"Getting started using crankd" has moved to nokyotsu.com/qscripts/2011/03/… – RobM Sep 17 '15 at 15:09

A launchd agent watching /etc/resolv.conf, and two network related .plist files under /Library/Preferences/SystemConfiguration/ seems to work for me (in Mac OS X 10.8.4):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>ifup.ddns</string>

  <key>LowPriorityIO</key>
  <true/>

  <key>ProgramArguments</key>
  <array>
    <string>/Users/Shared/bin/ddns-update.sh</string>
  </array>

  <key>WatchPaths</key>
  <array>
    <string>/etc/resolv.conf</string>
    <string>/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist</string>
    <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
  </array>

  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

I had previously only used /etc/resolv.conf, but there were cases where that wasn't enough.

share|improve this answer

The following works on Mac OS X 10.11.3 (El Capitan)

create a file: networkchange.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>networkchange</string>
  <key>LowPriorityIO</key>
  <true/>
  <key>ProgramArguments</key>
  <array>
    <string>/Users/Shared/bin/networkchange.sh</string>
  </array>
  <key>WatchPaths</key>
  <array>
    <string>/etc/resolv.conf</string>
    <string>/var/run/resolv.conf</string>
    <string>/private/var/run/resolv.conf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

And register it with:

launchctl load networkchange.plist
launchctl start networkchange

note that the script is run by the user registering the plist.

share|improve this answer

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.