Custom scripts executed as part of an action or alert profile under the script section will be executed when the monitor has failed all rechecks.
The scripts will execute any time an alert would normally be sent. This typically occurs when the monitor enters a hard failure state (WARN, UNKN or CRIT), any time the alert interval is exceeded or when the monitor recovers to the OK state.

Your custom scripts should be able to appropriately handle both outage and recovery scenarios.