Launchd – Run command on DHCP IP address change

Every once and a while I need to run a command when a clients IP address change.  In OS X it is really easy to do with launchd.  All you need to do is set a watch path for the DHCP leases folder “/private/var/db/dhcpclient/leases/”.  See the example below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>Label</key>
 <string>com.example</string>
 <key>UserName</key>
 <string>root</string>
 <key>ProgramArguments</key>
 <array>
 <string>PATH TO COMMAND THAT YOU WANT TO RUN</string>
 </array>
 <key>WatchPaths</key>
 <array>
 <string>/private/var/db/dhcpclient/leases/</string>
 </array>
</dict>
</plist>

You will want to replace the “Label” string with your own (typically in reverse domain notation ) and the “ProgramArguments” string with the path and options for the command/program you want to run.