简体   繁体   中英

PHP - Long running process on shared hosting environment

I have a question which I wasn't able to resolve. I'm developing a script, and I have a long running process, in a nutshell I'm scraping some data from external urls and after that I am adding that data to database, and that takes about 30 minutes or so.

I have used @ini_set( "max_execution_time", 1800 ); to allow the script to run long enough.

Something like this:

function calculator_get_games() {
   @ini_set( "max_execution_time", 1800 );

   $api = Api::getInstance();

   $api::get_all_games();
}

That works on my localhost. The problem is that, of course, doesn't work of shared hosting environment. The script brakes after certain amount of minutes and I get an 503 Error.

The strange thing is that I think it is not max_execution_time related since the script runs for a few minutes before it brakes, and max_execution_time on the server is set to 60 seconds. I read somewhere that I can use set_time_limit() function, to reset the counter or prolong the execution time of the script, but that didn't help, I got the same error after a while. I have also read that some companies on shared hostings set a limit for how long a PHP script can run for, non related to max_execution_time.

The thing is that the script could be used by various users and I wouldn't have control over the servers. So I'm hoping to find a way, to allow the script to run even on shared hostings like the one described above, if possible, so the users could use the script without issues on almost any hosting environments.

So, any help is appreciated. Any idea or alternative approach I can use.

Thanks.

EDIT:

Like Jamie_D Pointed out, I wasn't clear enough. I'm already using WordPress Cron in the background to start the script and update it every day. But it brakes at the same time as when i try to execute the script from the browser. I'm developing a plugin that a user could use on any server basically. On plugin activation, WP cron will start the scraping for data with file_get_contents and return the data after 30 minutes, which will be feed to the database. Hope that clarifies it a bit, sorry for the confusion, i'm still quite a beginner in some of these things.

If you need to execute a script that takes that long, I would consider a background process that does not rely on the calling script. Since you have not clarified your application intention it's not possible to point you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM