1

I have created a script on PHP that creates cache files from API and it takes around 30 minutes to load the page completely means when it creates all cache files.

I have a concern that my hostinger's customer support is telling me that it won't run for 30 minutes but in some answers, I found that it can run in the background and nothing to worry about until it's loaded.

So is that possible that the cronjob will run up to 30 minutes?

If not what is the best solution to run that cache making script at a specific time in the background like the cronjob does? Please Explain in brief so I can get a way.

Thanks for the great answer.

web90
  • 15
  • 5

2 Answers2

1

Ideally, for long running tasks, the task should be hosted in a platform that allows extended operations and defined in a way that it can be externally triggered, this might be in the form of an endpoint in a web API.

Then you can use the cronjob to trigger that process.

Without creating a whole API, you could make this a single endpoint on your website, a hidden page that only the cronjob knows how to call, then run your script from there.

There are lots of ways around this but the methodology is similar just use the cronjob as the trigger to a different process. Move the core logic of your script to a platform that allows the long execution time.


This is a similar post: Run a “long” php-script via Cronjob with an answer that suggests you can try to execute the script without waiting for the response, that is the same expectation with calling an external web process or API, the cronjob should not wait for a response.

Chris Schaller
  • 13,704
  • 3
  • 43
  • 81
  • _"make this a single endpoint on your website"_... trying to call something over HTTP that takes 30 minutes is not a good idea. Why not just execute `/usr/bin/php some-php-script.php`. Crontab executes tasks in the background anyway – Phil Jul 26 '21 at 07:05
  • @Phil I said not to wait for a response ;) some hosting plan and environments don't allow long running executions, as is indicated here by the help desk response. In those cases the execution on the host process will probably have a fixed timeout set meaning you need another process and possibly another host to execute the task. In a scenario where you have multiple cronjobs, I would still keep the trigger cronjob in the same management space, and have it call out to the external process. – Chris Schaller Jul 26 '21 at 07:34
0

It's good practice to limit resources on web server, especially in the shared hosting account. Because, in most cases, it may cause the web server to slow down and Denial of Services situation.

It's recommended to run the script using php-cli and cron. php-cli offer much more relaxation, such as time and resource limitation. Please also read
Events in MariaDB VS Cron in php - which is better

dwi kristianto
  • 159
  • 2
  • 11