I have a PHP script which takes a long time and makes many DB connections.

After about 5 minutes the server sends me an error 500 and the error\_log shows the following : 

```
(104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
(104)Connection reset by peer: mod_fcgid: ap_pass_brigade failed in handle_request_ipc function

```

I googled this and found that the obvious answer is to change either the "PHP\_FCGI\_MAX\_REQUESTS" variable inside the "fcgi-bin/php5.fcgi" file for the domain or "FcgidMaxRequestsPerProcess" variable inside the Apache config file, but I can't do either of these, as the server hosts multiple websites, all running with FastCGI.

I tried to catch the error 500 and redirect the page with the following PHP code :

```
register_shutdown_function('rerun');

$rerun = isset($_GET['rerun']) ? true : false;
main($rerun);

function main ($rerun=false) {
    // Lots and lots of stuff
}
function rerun() {
    if (error_get_last() != NULL) {
        header('Location: http://www.example.com/myscript.php?rerun');
    }
}

```

But it doesn't work either, I still end up with an error 500.

Does anyone have any idea on how to fix this ?
Either by declaring the FastCGI variables locally (i.e. for this website only), or, preferably, by correctly catching the error 500 and triggering the "rerun" function before it all crashes down.