Skip to content

Instantly share code, notes, and snippets.

@prehensile
Created November 14, 2010 04:41
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save prehensile/675916 to your computer and use it in GitHub Desktop.
Save prehensile/675916 to your computer and use it in GitHub Desktop.
A launchd daemon to execute a Python script when network configuration changes.
<?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>prehensile.networkwatcher</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/python</string>
<string>/Users/henry/Scripts/BigPicture.py</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration</string>
</array>
</dict>
</plist>
@GabLeRoux
Copy link

GabLeRoux commented Oct 27, 2017

Write this to /Library/LaunchDaemons/prehensile.networkwatcher.plist.

Make sure it has the correct rights:

sudo chown root:staff /Library/LaunchDaemons/prehensile.networkwatcher.plist

Launch the daemon

sudo launchctl load -w /Library/LaunchDaemons/prehensile.networkwatcher.plist

Notes

  • You can rename the file, ex; network.watcher.plist
  • You can execute whatever script you want, not only python like in the above ;)

@sheamusburns
Copy link

sheamusburns commented Nov 11, 2017

Hey @GabLeRoux I'm trying to get something like this to work -- I've adjusted the plist file a bit with my own paths, put it in the LaunchDaemons folder, changed the rights, and launched it.

I figure it should run if I turn off and on Wifi, (I think?), but it doesn't .
When I run launchctl list I see the file listed, but it doesn't have a PID, so does that mean it's not running properly?

Any thoughts on how I can get this to work?

// Figured this out - had a line in my .py script that was causing an error because I assumed the location of a reference file was relative to the .py location, but instead the script was running from the /Library/LaunchDaemons folder.

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