Solved

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

Posted on 2014-07-31
6
454 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 82

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 108

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now