Results 1 to 5 of 5

Thread: PHP CLI to run forever

  1. #1
    Join Date
    Sep 2009
    Beans
    6

    Arrow PHP CLI to run forever

    Hello,

    I've got a php script that runs in a while(true) loop and I want this script to run until I stop it manually, which can be a day, two weeks or five months.

    However, my php script breaks for some reason, and it's not the while loop.
    The script is using sockets (with fread()) if that is to any help.

    I don't use the cli very often, so my question is if there are any limits in the cli config that can cause this.

    The script alone is correct (stream_set_timeout for the socket, and set_time_limit for the execution time), so I figured that it must be in the cli.

    Any help is appreciated.
    Last edited by Creek; March 27th, 2010 at 08:22 PM. Reason: typo

  2. #2
    Join Date
    Dec 2007
    Location
    Dublin, Ireland
    Beans
    130
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: PHP CLI to run forever

    when it breaks, does it dump/print anything?

  3. #3
    Join Date
    Dec 2007
    Location
    Dublin, Ireland
    Beans
    130
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: PHP CLI to run forever

    php cli shouldn't be affected by max execution. to see how long it runs for, run
    Code:
    time php script.php
    . Look for consistency.

  4. #4
    Join Date
    Apr 2009
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: PHP CLI to run forever

    It is affected by max execution.
    You have to create a line in your script which sets the execution time:
    Example:
    Code:
    ini_set("max_execution_time", 90);
    Sets 90 seconds.

    Extra info: Everytime this line runs, the counter is reset. And only PHP-used-time is counted.
    Example:
    Code:
    <?
    $query="some query that takes about an hour to process";
    $i=0;
    do {
    mysql_query($query);
    $i++;
    }
    while ($i < 100);
    echo "Ready";
    ?>
    This will run perfect. The only problem in Windows can be Internet Explorer which has some self-defined-max-execution-time before it displays a "could not connect" error. Both Firefox and the command-line will even wait for the full 100 hours and the script wil keep running.

  5. #5
    Join Date
    Sep 2009
    Beans
    6

    Re: PHP CLI to run forever

    Thanks for the reply guys.

    When I run the CLI I do it with -n flag (without php.ini) and I run it with set_time_limit(0) which sets the max execution time to unlimited, so it's not that.

    It does not give any error when it breaks (which rules out memory allocation).

    So I think it's a null byte (EOF) being received in the stream, so I've changed the while(true) to a while(!feof($socket)) and an echo after the loop to see if it breaks.

    The script has now been running for two straight hours, so it's probably a php error, not a cli issue.

    Cheers.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •