Plesk 12 Schedule Task, Manually Force Email on Error

80 Views Asked by At

I'm trying to figure out how to make sure that I'm sent an email if an error occurs within the script that I'm calling via URL. I thought that by throwing a PHP Exception, the Scheduled Task would consider that a good reason to email me ... but no.

Is there a way to ensure that the Cron notifies me if the script itself throws and exception, or something similar?

try {
    $connect = $rets->Login();
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), PHP_EOL;
    throw new Exception("caught for demonstration");
}
1

There are 1 best solutions below

0
On

For Operation System exit code which is not equal to 0 is only good reason to understand that something goes wrong :-)

Scheduled task which executes your PHP script is looks like OS command which execute proper PHP binary:

/opt/plesk/php/7.0/bin/php /here/the/path/to/your/script.php

If exit code of this command is not equal to 0 you'll get email message.

Try to replace:

throw new Exception("caught for demonstration");

with:

exit(1);