3

Is there a way to tell PHP to run one last function, or somehow override some error handler in the event of a fatal error? I have a cron that has a main entry point of:

$obj = new obj();
$obj->run();
exit;

Would wrapping that in a try catch do the trick if I'm not explicitly throwing errors? All I want is for PHP to do something as simple as create a txt file containing the fatal error, or even just create an empty file named 'failed.txt', or something.

Thanks!

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Josh
  • 12,448
  • 10
  • 74
  • 118
  • @Charlie: What I'm trying to say is that if there is an unrecoverable error, how would I best make some indication of it, other than the standard error log, preferably from a function or the catch part of a block. I didn't mean how do I type a new function in between run(); and exit(); :P – Josh Jun 16 '11 at 18:18
  • 1
    Not all errors can be captured (Fatal errors in particular). But if you have error_log enabled and configured correctly every error should get logged there. – datasage Jun 16 '11 at 18:19
  • 1
    There are other better answers, so I'll just post this as a comment: in a pinch you could just `touch('/tmp/foo.lock')` and at the end delete it. If there was an error, then the file will still be there. – Matthew Jun 16 '11 at 18:22
  • possible duplicate of [Handle fatal errors in PHP using register_shutdown_function()](http://stackoverflow.com/questions/4410632/handle-fatal-errors-in-php-using-register-shutdown-function) – netcoder Jun 16 '11 at 18:28

6 Answers6

3

error_handler might help you here

Or this for fatal errors: http://insomanic.me.uk/post/229851073/php-trick-catching-fatal-errors-e-error-with-a

Romeo M.
  • 3,218
  • 8
  • 35
  • 40
  • But the documentation says "The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called." I was hoping for a way to do something after a fatal error, which is E_ERROR, right? – Josh Jun 16 '11 at 18:21
3

You write it's cron. Call the script with a path to a new php.ini file wherein you make PHP to log the errors to STDERR or STDOUT.

php -h

will give you all commandline options.

Cron will send you the reports then if something failed by email.

Alternatively you can set this up to log into your own log as it's a new php.ini. In case that's easier for you to review.

hakre
  • 193,403
  • 52
  • 435
  • 836
2

You can set a custom error handler for most errors, and execute some code there.

But cannot handle an E_ERROR which is what your fatal error is likely to be.

Imagine if that error was for running out of memory. You try to handle that error, and end up using more memory. Which just throws another error, and you're right where you started.

ben
  • 1,946
  • 2
  • 18
  • 26
1

You can do custom error handling in PHP. Check the manual at: http://php.net/function.set-error-handler. This is a fairly straightforward application of that.

DaOgre
  • 2,080
  • 16
  • 25
  • But the documentation says "The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called." I was hoping for a way to do something after a fatal error, which is E_ERROR, right? – Josh Jun 16 '11 at 18:21
1

Use register_shutdown_function() to register a function to execute as the last call. This will get call whether there was an error or not.

See http://php.net/manual/function.register-shutdown-function.php for more information.

Francois Deschenes
  • 24,816
  • 4
  • 64
  • 61
  • That is what I ended up doing, so thanks, but Romeo had answered it first, so sorry about that... – Josh Jun 16 '11 at 20:54
0

Use

try 
{ 
    //Code
} 
catch(Exception $e)
{
     //Execute on error
}
Ortiga
  • 8,455
  • 5
  • 42
  • 71
  • so no matter what, be it a file not found, a MySQL too many connections, or anything, it will run the finally? – Josh Jun 16 '11 at 18:22
  • finally will execute even if there's no error. What you want is a generic Exception catch, that will execute on any error. Updated my answer – Ortiga Jun 16 '11 at 18:24
  • There is no finally in PHP AFAIK. – hakre Jun 16 '11 at 18:24