how can i make my web server allow my php script to run for 75 minutes instead of disconnecting after only 45 minutes?

The problem is because of shared server hosting limitations. The shared server does not allow any script to run for more than 45 minutes (VPS & dedicated run normally).

I get around this issue by run 2 separate cron jobs (40 minutes between every job) instead of one big one.

  • The 1st job creates a file and saves data to it.
  • The 2nd job appends data onto the same file for the rest of stream duration.

Every script works for 40 minutes only in order to avoid the server killing the script.

Here is my final code:

<?php

// Ignore user aborts and allow the script to run forever
ignore_user_abort(true);
set_time_limit(0);

$path = __DIR__ . '/save.mp3';

$stream = fopen( 'my url', "rb" );
$save = fopen($path, "a");
$startTime = time();
$finishTime = $startTime + (60*40);
while ( $finishTime >= time() ){

    $response = fread( $stream, 8192 ); 
    fwrite($save,$response);
}

fclose( $stream );
fclose($save);
exit();

?>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top