user1492948
user1492948

Reputation: 13

PHP ends script execution automaticly

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?

Upvotes: 1

Views: 125

Answers (3)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

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

Upvotes: 1

Luc Franken
Luc Franken

Reputation: 3014

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?

Upvotes: 0

Kuba Wyrostek
Kuba Wyrostek

Reputation: 6221

You could use php function set_time_limit().

Upvotes: 0

Related Questions