Trigger action on external monitor plug/unplug

There is ControlPlane, but I didn't have much luck with it on El Captain.

Other option is running a never-ending AppleScript which calls your script on change of number of monitors. See this answer.