Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@GabLeRoux 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

This comment has been minimized.

Copy link

@sheamusburns 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
You can’t perform that action at this time.