PDA

View Full Version : Anyone able to run [php] scripts online longer than 60 seconds?



heepofajeep
06-03-2009, 06:51 PM
Anyone able to run a php script online for more than 60 seconds?

If so, what kind of account do you have?


Thanks!

JamesYap
06-04-2009, 04:34 AM
The default run time is 30 second and if you want to run more than 30 seconds, all you need to do is put up your own php.ini in the public_html directory and activate it under PHP Configuration in CPanel.



max_execution_time = 90 ;


You can change a lot of settings, for example, you can even increase the memory from 32MB (default) to 128MB or more


memory_limit = 128M ;

heepofajeep
06-04-2009, 04:19 PM
So once you change the ini, are you indeed able to run scripts longer than 1 minute?

What plan do you have?


Thanks!

Early Out
06-04-2009, 05:48 PM
There is essentially only one "plan" on BH.

There is a "high-CPU" option, but BH doesn't advertise it, and it's not really worth bothering with. If your scripts aren't optimized and/or your database isn't indexed properly, the high-CPU plan isn't really going to solve the problem.

Other than that, one size fits all on BH.

heepofajeep
06-04-2009, 05:55 PM
Thanks, but that still doesn't answer my question.

felgall
06-04-2009, 07:53 PM
The answer would depend on how much of the CPU that the script needs to run.

JamesYap
06-05-2009, 01:27 AM
There are usually 2 reasons why script need a longer time to run.

1. Waiting for slow responses, for example - fetching files from other servers. This won't take up a lot of CPU and Memory. So no problem.

2. A busy loop that keep on calculating something to calculate the DNA sequence in an attempt to win the nobel prize! This is CPU intensive and will give your trouble!

wysiwyg
06-06-2009, 06:05 AM
A script may need to run indefinitely if it's listening for an event, or if it is required to sleep for an amount of time before continuing.

Those actions require a minimal amount of resources.

Early Out
06-06-2009, 06:24 AM
On a shared IP, no process can run for more than 5 minutes (300 seconds). For longer than that, you need a dedicated IP address (which you'd need anyway if you're listening for an event on a port, for example).

Knowledgebase article (http://helpdesk.bluehost.com/kb/index.php/kb/article/000478).

felgall
06-06-2009, 04:38 PM
Of course a more efficient way of listening would be to have the script do a quick check to see if the event it is waiting for has occurred and if it hasn't then stop running. A cron job could then rerun the script at regular intervals with the script only performing actual processing if the event has occurred since it hast ran. That way the script can effectively wait indefinitely while only running for a very small fraction of a second.

wysiwyg
06-07-2009, 11:38 PM
Of course a more efficient way of listening would be to have the script do a quick check to see if the event it is waiting for has occurred and if it hasn't then stop running.

Big Bang -> Execute Code -> Universe
???????? -> Universe -> Execute Code

Big difference.

felgall
06-08-2009, 12:41 AM
Big Bang -> Execute Code -> Universe
???????? -> Universe -> Execute Code

Big difference.

Not if you invert the code. Then they become identically equivalent

wysiwyg
06-08-2009, 01:12 AM
Really? You know the big bang happened because the universe exists?

That's not the same as detecting an event.

Regardless, not all events cause a permanent change of state, so trying to see if they happened after the fact is impossible.

felgall
06-08-2009, 01:12 PM
Regardless, not all events cause a permanent change of state

All events can be made to cause a permanent change of state though.

Early Out
06-08-2009, 01:18 PM
All events can be made to cause a permanent change of state though.
This discussion is getting really metaphysical. What is truth? Is anything real? :D

JamesYap
06-08-2009, 01:48 PM
How to use PHP script to listen to an event?

felgall
06-08-2009, 03:19 PM
How to use PHP script to listen to an event?

Since events generally happen on the client rather than on the server you would generally set up an ajax call so that when the event on the client is triggered that the ajax calls the appropriate PHP script.

One event that cen actually be detected server side is the arrival of a particular time on the server and for those you can use a cron job to trigger the appropriate PHP script.

The only other events I can think of that can happen server side relate to updates either to data on the current site or updates to some other site and in each of those cases the code you want triggered can either be directly attached to whatever code is performing the update or you can run a cron job at regular intervals to test for when the update occurs.

JamesYap
06-09-2009, 01:12 AM
Since events generally happen on the client rather than on the server you would generally set up an ajax call so that when the event on the client is triggered that the ajax calls the appropriate PHP script.

That means PHP script itself is not 'event orientated'. :D We have to rely on a client side script - Javascript. PHP script will not be active until it is called by the Javascript. PHP alone can't stay alive to listent to an event.

felgall
06-09-2009, 02:34 AM
That means PHP script itself is not 'event orientated'. :D We have to rely on a client side script - Javascript. PHP script will not be active until it is called by the Javascript. PHP alone can't stay alive to listent to an event.

The only way the PHP is going to "hear" the event after the page displays is from an ajax sall in the JavaScript. Since that starts running a new PHP to handle the event there is no need for keeping a PHP script alive to listen for it since the existing PHP isn't going to receive the call anyway as a new execution will be started by the call.

The only "events" that can happen server side involve running a script to cause the ev ent and so that script can just do everything it needs to as a part of the event.

In those situations where that isn't possible you can set up a cron job to handle testing for the "event" at regular intervals. That would normally only be necessary where an event involves more than one user or more than one web site.

wysiwyg
06-09-2009, 08:32 AM
I'm not going to get too much into it because you wouldn't normally do this on a web server, but PHP has COM functions which basically allow it to communicate with other processes.

You can use a loop (eg. while statement) to keep the script alive while it waits for something to happen.

To use a real example, I wrote a bot in PHP which attaches itself to Skype running on my computer. It captures events such as someone calling me or sending a chat message and reacts by executing a function (automatically picking up or replying with a message).

felgall
06-09-2009, 01:18 PM
I'm not going to get too much into it because you wouldn't normally do this on a web server, but PHP has COM functions which basically allow it to communicate with other processes.

As you say, that sort of use goes beyond web hosting into other areas of the internet. As BlueHost only allow the use of their hosting space for web hosting you would not be able to use the hosting space for scripts performing functions like that on BlueHost. You should probably be looking at a VPS or dedicated hosting plan if you wanted to do something like that.