-1

I have some limitations with my host and my scripts can't run longer than 2 or 3 seconds. But the time it will take to finish will certainly increase as the database gets larger.

So I thought about making the script stop what it is doing and call itself after 2 seconds, for example.

Firstly I tried using cURL and then I made some attempts with wget. But there is always a problem with waiting for the response and timeouts (with cURL, for example, I just need to ping the script, not wait for a response) or permissions with the server (functions that we use to run wget such as exec seems to be disabled on my server, or something like that).

What do you think is the best idea to make a PHP script ping/call itself?

Victor Ferreira
  • 6,151
  • 13
  • 64
  • 120
  • This doesn't make any sense. Aside from that, why would you pay money for a hosting environment that doesn't meet your needs? You can get a VPS on digital ocean or the like for $5/month and do whatever you want. – But those new buttons though.. Jun 03 '15 at 15:53
  • Sounds like you are outgrowing run-of-the-mill webhosting, and you're ready for a virtual server, where you won't run into these kinds of limitations. Virtual servers (also known as 'cloud servers') start around $10-$20/month from providers like Rackspace, AWS, Digital Ocean, etc. – mti2935 Jun 03 '15 at 15:53
  • 1
    If the script is running itself, that's going to make the original script run for longer than two seconds which will get killed by your host. Move hosts or look at cron jobs and see if they'll support those running longer than 2 seconds. – tbddeveloper Jun 03 '15 at 15:54
  • Making a fire-and-forget is certainly possible in PHP, close-header, ignore_user_abort, that sort of stuff. This is not what you want to be doing though. 3 seconds in unreasonably short and completely arbitrary. – Halcyon Jun 03 '15 at 15:58
  • my client pays for that. i do my job that is suggesting alternatives and finding solutions. I hope this isn't the kind of 'final answer' you give to your clients. it's like suggesting an airplane for someone who asked about the fastest way to get somewhere by car. @Halcyon I actually though of something like that. is it just like writing a header at the beginning? could you explain a little more? 3 secs was just an arbitrary number I said. and it is probably just the default configuration. i could probably by opening a ticket on CP increase this value for a few more – Victor Ferreira Jun 03 '15 at 16:10
  • 1
    @VictorFerreira this is not the answer you give to your customer but the answer programmers among each other would give. If your customer doesn't want to pay for a reasonable server then his database is limited by how long your script takes. It's completely normal business practice to suggest upgrading. We're not talking about cars and airplanes, think more like scooter and motorcycle. If you want to go long distances you want a motorcycle. – Halcyon Jun 03 '15 at 16:13
  • my question didn't include changing hosts as a possibility and I didn't ask for the best way to run a long script. I know buying a better host will help, but this is part of the problem: the host must remain the same. asnwers like that would be more than acceptable if they started with "this isnt possible at all. you have to change hosts because there is no approach for what you need". someone voted me down and this is insane, what is the point?! the question is relevant and specific but some nice guy think changing hosts is the only acceptable possibility – Victor Ferreira Jun 03 '15 at 16:21
  • I voted you down because your question makes no sense. The first problem is you can't have a script call itself and not be running at the same time. In order to call itself it has to be running while it's waiting to call itself again, so you've still got a long running script. In fact you've got a longer running script than before. Then, when it does call itself it will then spawn more processes calling more instances of itself and on into infinity. – But those new buttons though.. Jun 03 '15 at 17:04
  • The bottom line is if you have a script which might take 2 or 3 minutes to complete and your host imposes a 2 - 3 second rule, there's no way around that. Your choices are (1) Write a script that completes faster, (2) change hosts, (3) Fire the client and go for a beer. – But those new buttons though.. Jun 03 '15 at 17:06
  • @billynoah since it is a question it is acceptable that I am not completely sure how to achieve some result. a question could clarify other people and ourselves even when the answer is "it is not possible, there is a limitation on this technology". it makes sense because it isn't a paradox or something completely nonsense. a down vote is a punishment. besides, there seems to be a "solution" that you write a header back to the client that will finish the blocking of the main script. I was waiting for more alternatives from you all because you rock. – Victor Ferreira Jun 03 '15 at 17:10

2 Answers2

0

On Unix/LInux systems I would personally recommend to schedule CRON JOBS to keep running the scripts at certain intervals

May be this SO Link will help you

Community
  • 1
  • 1
Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • that won't help him if the problem is the amount of time his script runs exceeds some provider imposed limit. – But those new buttons though.. Jun 03 '15 at 15:54
  • @billynoah indeed. I use cron to start the script every hour. but then I have no control about how long it takes to finish the job. it will be 5 or 10 seconds in the first months. but 2 or 3 minutes in an year – Victor Ferreira Jun 03 '15 at 16:13
0

Php scripts generally don't call other php scripts. It is possible to spawn a background process as illustrated here, but I don't think that's what you're after. If, so you'd be better off using cron as was discussed above.

Calling a function every X amount of seconds with the same script is certainly possible, but this does the opposite of what you want since it would only extend the run time of the script in question.

What you seem to be asking is, contrary to your comment, somewhat paradoxical. A process that calls method() every so often is still a long running process and is subject to the same restrictions as any other process on the server, regardless of the fact that it may be sitting idle for short intervals.

As far as I can see your options are:

  • Extend the php max_execution_time directive, or have your sysadmin do so if they are willing
  • Revise your script so that it completes within the time limit
  • Move to a new server
Community
  • 1
  • 1