To call a function on script exit, use the register_shutdown_function in PHP.
function shutdown_handler() {
// Send an email, notification, etc.
}
register_shutdown_function("shutdown_handler");See the official documentation, as well as some Stack Overflow posts.