PHP Timeout on Windows/IIS

Have you ever run a PHP utility script on a Windows/IIS platform and had it unexpectedly timeout? Your IIS settings for script timeouts might be conflicting with your PHP settings.

Let's say your script is querying a remote, third-party web service for share data on 3000 articles on your site, and for whatever reason, the web service only allows you to query share data for one URL at a time. This could take a while. So on your Windows/IIS server, you set up a scheduled task to run at 1:00 am to retrieve all this data and put in your database for convenient access.

Within your PHP script, you've already adjusted the PHP settings so that your script can run for at least an hour without interruption:

ini_set('max_execution_time', 3600);
//ini_set('memory_limit', '1024MB');
ini_set('memory_limit', '1024000000');

//Note:  Another reason your memory_limit setting might not be working is
//because you need to define it in terms of bytes, as I did above, instead of in
//megabytes, as in the line commented out above.

And yet your script is timing out and displaying a 500 error after just 15 minutes. The problem? Quite possibly an IIS setting is ending your script too early.

So put on your sysadmin hat (or talk to your sysadmin) and open up IIS Manager. Click on the server name (not an individual site name--this has to be done at the server level) in the left-hand navigation menu and click on the Fast CGI Settings icon. Double-click on the php-cgi.exe line to open up the dialog box.

In the dialog box, under Process Model, look at the Activity Timeout setting. This setting could be timing out your PHP script early change it to match your PHP setting for max_execution_time (3600 seconds, for example). Click OK, and now your PHP script should run and complete like a champ.

Tags: