1

Firstly I want to give you the basic idea of what I am trying to do:

I'm trying to make a free web hosting service do some work for me. I've created one php page and MySQL db. The basic idea behind my PHP page is I have a while loop with condition of $shutdown, and some counter inside while loop to track whether code is running or not

<?php

/*

Connect to database etc. etc

*/

$shutdown = false;
// Main loop
while (!$shutdown)
{
    // Check for user shutdown request
    $strq = "SELECT * FROM TB_Shutdown;";
    $result = mysql_query($strq);
    $row = mysql_fetch_array($result);
    if ($row[0] == "true")
    {
        $shutdown = true; // I know this statement is useless but nevermind
        break;
    }

    //Increase counter
    $strq = "SELECT * FROM TB_Counter;";
    $result = mysql_query($strq);
    $row = mysql_fetch_array($result);

    if (intval($row[0]) == 60)
    {
        // Reset counter
        $strq = "UPDATE TB_Counter SET value = 0";
        $result = mysql_query($strq);

            /*

        I have some code to do some works at here its not important just curl stuff

            */

    else
    {
        // Increase counter
        $strq = "UPDATE TB_Counter SET value = " . (intval($row[0]) + 1);
        $result = mysql_query($strq);
    }

        /*

    I have some code to do some works at here its not important just curl stuff

        */

    // Sleep
    sleep(1);
}

?>

And I have a check.php which returns me the value from TB_Counter.

The problem is: I'm tracking the TB_Counter table every second. It stops after a while. If I close my webbrowser (which I called my main while php loop page from) it stops after like 2 minutes. If not after 5-7 mins I get the error "connection has been reset" on browser and loop stops.

What should I do to make my loop lasts forever?

3 Answers 3

1

You need to allow PHP to execute completely. There is an option in the PHP.INI file which says:

max_execution_time = 30;

This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30. When running PHP from the command line the default setting is 0.

The function set_time_limit:

Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.

To check if PHP is running in safe mode, you can use this:

echo $phpinfo['PHP Core']['safe_mode'][0]

If it is going to be a huge process, you can consider running on Cron as a CronJob. A small explanation on it:

Cron is very simply a Linux module that allows you to run commands at predetermined times or intervals. In Windows, it’s called Scheduled Tasks. The name Cron is in fact derived from the same word from which we get the word chronology, which means order of time.

Using Cron, a developer can automate such tasks as mailing ezines that might be better sent during an off-hour, automatically updating stats, or the regeneration of static pages from dynamic sources. Systems administrators and Web hosts might want to generate quota reports on their clients, complete automatic credit card billing, or similar tasks. Cron has something for everyone!

Read more about Cron

Sign up to request clarification or add additional context in comments.

8 Comments

Okay I tried to set it set_time_limit(0); but it didn't work. Maybe server doesn't allow me to set the time limit? Is there a way to get the time_limit after I set it to check whether I'm allowed to make change?
Yea, now try running the script, any difference?
Is PHP Running in safe_mode? In that case, this method won't work!
Free and low-cost web hosting services impose their own PHP max_execution_time limits beyond what is set in php.ini or set_time_limit. They do this to keep customer code from running away; it helps them conserve their processor resources. You can't really do what you're trying to do.
I think I have to dive in Cron thing :) Thank you guys
|
0

You could use php function set_time_limit().

2 Comments

I actually tried it // Set Time set_time_limit(0); But didn't work. It still stops
It is possible and hihgly probable that server administrator disabled this function by running php in safe-mode. Please verify it with phpinfo().
0

You should not handle this from a browser. Run a cron every minute doing the checks you need would be a better solution.

Next to that why would you update every second? Just write down a timestamp so you know when a request was made?

Making something to run forever is not doable. More important is to secure that your business process keeps running. So maybe it would be wise to put your business case here, you seem to need to count seconds and do something within a minute but it's not totally clear. So what do you need to do?

3 Comments

To give you a start, on a linux machine just enter crontab -l, that will give you a list of existing cronjobs. Likely none. You enter this in for example an SSH session so a terminal. Then enter crontab -e, that will allow you to edit the crons. It is a list of tasks on a specific time, for example every minute. If it is PHP you have to make the file executable with for example chmod 0755 (check right permissions!) and you have to add a path to PHP like /usr/bin/php to the beginning of the file. Check path on your environment.
Okay the problem is I'm trying to do this works for free. There is a lot free web hosting services but I don't know how to enter these commands to their linux machine? Haha I think I need to have a VPS for this? Don't I?
Depends, sometimes you can set them also in some control panel, ask the hosted for it or use another way. Even SSH is sometimes possible. Actually if you start applications like this you will always run into this issue. Maybe use something like the free instance of Amazon AWS? aws.amazon.com/free Just an idea, not sure whether it's totally free but won't be expensive as I expect anyhow. You get there the access you need so that might solve the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.