Is there any way to run a script when a network interface comes up in Mac OS X?

I've tried putting a script named ip-up in /etc/ppp but it doesn't seem to fire when I disconnect and reconnect to a wireless network.

I'd prefer not to have to run a cron job to constantly check whether the network status has changed. In some linux distros, you can accomplish this by putting a script in /etc/network/if-up.d/ . Is there anything similar for Mac OS X?


You could try MarcoPolo or one of the similar utilities listed on its website. Location Changer looks promising if you're a minimalist.

| improve this answer | |
  • 1
    I'd prefer to do this without installing additional programs. However, the Location Changer link was very helpful since it includes a launchd configuration for running the program on a network change. – Zxaos Oct 20 '10 at 14:01
  • 1
    Zxaos: Sure, that's why I included it. It's basically a launchd/bash script template for whatever you want. Saves you from writing the boilerplate code yourself. – Daniel Beck Oct 20 '10 at 15:09
  • if I can bother you to look at this very-relevant q (superuser.com/questions/265861) I'd much appreciate it (how to start and stop vpn from command line on OSX). Excuse the soliciting, I'll delete this comment in a few hours regardless. Thanks in any case! – Dan Rosenstark Apr 2 '11 at 20:45
  • 1
    @Yar Not a problem, but it's past midnight in central Europe right now, so please wait half a day or so before pinging me again (and please do so if nothing comes up in the meantime, it's an interesting question). – Daniel Beck Apr 2 '11 at 23:15
  • Thanks @Daniel Beck. As you perhaps already saw, using Applescript was the answer. It probably is to many things on OSX, but I often forget it. – Dan Rosenstark Apr 3 '11 at 13:34

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" \
<plist version="1.0">





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

| improve this answer | |
  • This was very helpful to me, but there's an error (found by [this answer][1]): the opening <plist version="1.0"> tag is missing. [1][apple.stackexchange.com/a/181127/56862] – LiberalArtist Apr 16 '15 at 4:53
  • @patrix (and LiberalArtist): Thanks for the correction. I hadn't noticed, because it worked for me in 10.8.5. I guess later versions are more strict. – mivk Apr 18 '15 at 15:16
  • @mivk What exactly is supposed to change in these SystemConfiguration files? If I plug/unplug a network cable, nothing seems to change at all – IceFire Sep 24 '18 at 14:23
  • This seems close, but if I turn wifi off and back on, then the program only runs when the network disconnects, not when it reconnects. I do see that the /etc/resolv.conf file is deleted and readded when my wifi state changes, so I'm not sure why it's only running on disconnect. – Matt Huggins Oct 3 '19 at 17:47

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.

| improve this answer | |
  • Your blog is protected from anonymous reading. Please consider duplicating the content here. – nfirvine Apr 30 '15 at 17:49
  • Sorry about that. My blog has moved, so I've just updated the url. Feel free to edit the answer if you feel that some of the information from my post could be migrated into this answer. – Juan A. Navarro May 9 '15 at 16:37

This seems to work in bash:

(echo -e "n.add State:/Network/Global/IPv4\nn.watch" & cat) | \
 scutil | \
 awk '/notification/ {system("echo change")}'

replace echo change with your command, keeping in mind that if you need to quote anything in your command, you'll need to use '\'':

(echo -e "n.add State:/Network/Global/IPv4\nn.watch" & cat) | \
 scutil | \
 awk '/notification/ {system("echo '\''&'\''")}'
| improve this answer | |

ControlPlane: “Context Sensitive Computing”

ControlPlane is a direct port of MarcoPolo and in fact, much of the configuration from MarcoPolo still works with ControlPlane, just better! ControlPlane supports 32 and 64bit Intel based Macs running Snow Leopard and higher.

enter image description here enter image description here enter image description here

| improve this answer | |

Start with launchd. You might be able to similar functionality using it.

EDIT: 9 years later, maybe not. According to this Wikipedia entry:

In 2014, with OS X 10.10 and iOS 8, Apple moved code for launchd to closed source libxpc

| improve this answer | |
  • launchd seems like it's a step in the right direction, especially if I can figure out how other programs use it to detect network changes. – Zxaos Oct 20 '10 at 13:58
  • @Zxaos, launchd seems like a step in the right direction, but there is no reliable way to make it detect network changes. See my answer for a solution using crankd instead. – Juan A. Navarro Mar 24 '11 at 13:06
  • The URL leads to a 404 page. – Slaven Rezic Sep 26 '19 at 13:03
  • @Slaven-Rezic Updated answer and removed old link. – CaseyIT Sep 27 '19 at 14:07

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.