Script execution time

by 21 replies
25
I have a php script that takes around 15 minutes to run. But the server cuts it off around the 5 minute mark. Is there a way to avoid the runtime error in php?
#programming #execution #script #time
  • set_time_limit($seconds);
    You can find more detailed information about this function here:
    php.net/manual/en/function.set-time-limit.php

    Hope this helps!
    • [ 1 ] Thanks
    • [1] reply
  • You need to have root access to your server. Increase the value of max_execution_time in your php.ini file.
  • A script that takes 15 minutes to run is IMHO not a good idea!
    Too many things can go wrong in that time (internet connections, server performance etc).

    It it possible to change the script so that it processed smaller blocks of data at a time, but ran more frequently.

    Just a thought

    Bruce
    • [ 1 ] Thanks
  • I believe you can set this in the .htacess aswell
  • What kind of script are you running that takes 15 minutes to run?

    If it runs that long, there is either an issue or it is not programmed correctly.

    If it is doing a large task like backups or emails, then it should be limited with a timeout then restarted where it left off. But this should have been coded in if the script was coded correctly.
    • [1] reply
    • I think if you run the script from the command line, the set max_execution_time doesn't apply. Can anyone confirm this?
  • Any script that runs beyond 20 seconds needs a rework. If you are good at programming, try refactoring to make sure that it does not timeout. Again, if possible get someone else to help you review, improve and remove the bottleneck that's making it take that long to execute.

    Good luck
  • You should investigate why the script is running so long, to me it is not normal.
  • Could you give us an idea of what the script does?
  • Yes you can increase runtime in php ini
    • [1] reply
    • Increasing runtime is NOT the solution. The script needs refactoring. I've been programming for the last 9 years and have never seen anything that runs that long.

      Rewriting the script is the only solution otherwise your hosting company will eventually send you a warning.
  • There must be something wrong with that script, unless you handle millions of records. There is no way that a normal script can take that much time...

    Have a look at it, if it runs for so long it will consume so much CPU time that you hosting provider might send you a warning, as indicated by hhunt.
  • I don't think we can speculate saying that it shouldn't be running that long etc until we know what it does..... if it's on the client end (highly unlikely) obviously it shouldn't run that long as otherwise the client wouldn't stick around lol..

    But it sounds like he is running it for processing something on his end, could be processing a lot of data from the database... which could very well then run for 15 minutes.
    • [1] reply
    • It doesn't matter if it is processing millions of database entries.
      It should never run more than a few seconds.
      This is why it is timing out which is a server protection to keep a script from overloading the system.

      Its the same as a PC only allowing a program a set amount of memory.
      If it is allowed to much memory there will not be enough left for the PC to run, and it will lock up or crash. Servers are no different.
  • A dataset that takes 15 minutes to process should not be done in PHP. It is comparatively slow compared to other languages like Perl or C##, and not what it was designed for.

    You would be better off to process the data in the background with another program, then view the results on a web page powered by PHP.
    • [1] reply
    • hi,

      This is not fair to execute a script for 15 minutes, modify your code to break the huge task into smaller task nd process it simultaneously. Changing execution time in .ini file is not the proper solution for it.
  • I wouldn't really recommend running multiple copies of the script over the one..... obviously you can run multiple copies to reduce the runtime, but I wouldn't go crazy.

    If you're on a dedicated server, this is going to depend on the amount of cores you have on your system; if you add too many it may run slower over running a smaller amount of longer running scripts because of context switching in the processor between the threads.

    If you run the process in the background you can also set a lower priority to these processes so that other services like apache can still run seamlessly.
  • A script that runs over 20 seconds needs to be reworked and you should not be attempting to reframe it unless you are a programmer.
  • Banned
    [DELETED]
  • Why does everyone assume just because it takes 15 minutes that it means the script is bad? Sure I've written some bad code that took a long time to update or to process but if that's where you are as a programmer then that's where you are.

    I agree that the program should be reworked and optimized if possible, but if not then keep it how it is and move on. There will be opportunities to fix it later. Depending on how your script is executed will determine how and what you can update.

    Check out the command for ini_set. This allows you to modify settings for your specific script. You cna also run phpinfo() on your page to double heck the settings for that script.
    • [1] reply
    • If the OPer is in a shared hosting environment (or even some VPSs), a 15-minute execution time will earn them a huge hand slap if not outright suspension of the account - not to mention that it probably won't even complete.

      There is no reason for a PHP script to run 15 minutes. NONE. 60 seconds is even pushing it.

      I didn't say it was a bad script (maybe someone else did, I don't know) - what I did say is that this much processing needs to be moved to a Perl/Python (or whatever) script and run in the background, not as a sub process of the web server software.

Next Topics on Trending Feed