Premature end of script headers 500 error

software development

#1

I’m having trouble writing a php CRON script that will be used to get data from an API. I get a 500 error and log entry “Premature end of script headers: myfile.php” whenever running it. Simplified, the script is the following (which still produces the error).

[php]

<?php //myfile.php ini_set('max_execution_time',60); //phpinfo(); $i=1; $time = 45; while ($i < $time) { echo $i . '
'; //call API sleep(1); //mandatory sleep call $i++; } //update database ?>

[/php]

This is not memory intensive and shouldn’t time out…it has me completely stumped.

I am using the default php settings and the load of my shared server is
21:02:47 up 51 days, 22:27, 1 user, load average: 4.65, 4.24, 4.07.

Thank you,
Dave


#2

Your script is taking too long to start generating any output, and the web server is assuming it’s broken. Offhand, I think it gives up after about 30 seconds.

You may be able to stave off the timeout a bit by outputting a header, even a meaningless one:
[php]header(“X-Hang-On: a little bit while I do stuff”);[/php]


#3

Unfortunately, the bogus header did not change anything.

I forgot to unschedule the CRON job so it ran late last night. It completed successfully then and when I ran it via CRON again this morning. But when I test it through a browser, it fails same as before. So does the server put different limits on CRON jobs? I’d assume CRON jobs wouldn’t have such limits since they often will not produce output.

In time, this script will only be executed through CRON, so should I not even worry about this?


#4

I’m fairly certain Dreamhost automatically kill “long running processes” regardless of how you configure PHP. Use of memory may also contribute to the process getting killed. I’ve definitely had experience of this.


#5

Memory is the primary factor we look at. Absent any other factors, long-running processes only get killed after sitting around for several days.