Solved

I need to run a PHP script for a while - how Does PHP Maximum Execution Time Work?

Posted on 2014-07-31
6
458 Views
Last Modified: 2014-08-06
I have a PHP script that I am testing that works as it should and completes in a little under 3 minutes with a certain size data input test file.

When I move to production, the data input file could potentially be 15 times this large (making execution almost 45 minutes for a million records.)

What do I need to consider? I'm not sure why the script is not being interrupted now - in Webmin > PHP Configuration > Resource Limits, my Maximum Execution Time is set to 30 seconds, and the Maximum Input Parsing Time is set at 60 seconds.

Am I considering all PHP Config options involved here?
What should I increase these options to?
What pro's/con's do I need to consider if I do increase these parameters?
0
Comment
Question by:FirstDirect
6 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40233482
PHP max execution time doesn't apply to PHP on the command line, only when running thru a web server.  

http://php.net/manual/en/info.configuration.php#ini.max-execution-time

http://php.net/manual/en/features.commandline.differences.php
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40233961
You can set and reset the time limit at any time.  If you put this into a loop, you may be able to create a long-running job.
http://php.net/manual/en/function.set-time-limit.php

I emphasize may because PHP is not really made for what you're describing, and the server that is running the PHP script may have time limits, too.  What is the application and why does it take so long to run?
0
 
LVL 29

Expert Comment

by:fibo
ID: 40234101
Problem with running a long job is that it might gobble a significant part of the available CPU resources: that's why there are these limits: if a web site or some other process is running on the machine... it might slow down for a long time.

We have some jobs that need lots of cpu cycles: we have chosen that they will run only 5 sec (which is in fact HUGE!), but have put in place some kind of "progress counter" which allow the job to resume later.
With a cron job, we run this every 15 minutes: we are them pretty sure that user experience on our sites in not impacyed heavily.

Usually such jobs are, as in our case, database related: use transactions (so that your data is kept consistant even if the program is interrupted) and update the counter just at the end of the transaction.

That's the idea, now as Ray suggested you should explain what you want to achieve: brute force computing? batch updates of data?
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 58

Expert Comment

by:Gary
ID: 40234287
Check you have the right columns indexed and not columns that don't need it
Optimize your sql
What are some samples of your queries?
0
 
LVL 11

Expert Comment

by:Murfur
ID: 40234295
To add to DB's comment, this sounds like a cronjob waiting to happen!

I would create a results table in the database and get the cronjob to post the results of the lengthy query to the results table. That way a site page can simply query the results table in a fraction of the time.

Commonly a cronjob will use wget to call a web page but wget will timeout just as a browser so as Dave has said, you use PHP on the command line. The caveat is that this needs to be run on the same server as the web site so that the php command refers to a local file e.g. this:

* */2 * * * php /www/sitename.com/html/filename.php  >/dev/null

One thing to consider is how often you want to re-run the query to update the results which is probably  driven by how often the source data changes. If it the query takes 45 mins to generate results then a frequency of a few hours may seem appropriate and probably not less than 2. The example above would run every two hours and subject to your server configuration would only "reply" if there are errors to report i.e. cron failed to run the job but there is nothing to stop you making your own reporting service within the php file. For example, at the simple end of things, you only need to echo result statements and these will typically be emailed to the user account who is running the job. Or you can build your own more complex process to use sendmail etc. to deliver a more complexly structured message such as a summary of the query result or formatting the email content so that the company execs get a graph of sales figures etc. The world is, as they say, your oyster. Code away...

Finally, you mention "Webmin > PHP Configuration" so I'm guessing that you are using a GUI front end to administer the server like Webmin or PLESK. If so then look for the cronjob options or "scheduled jobs" in there which will give you an easier method of administering the schedule.
0
 

Author Closing Comment

by:FirstDirect
ID: 40244394
Thanks, Dave.
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question