run a php in the background using http

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WebRod

    run a php in the background using http

    Hi,

    i have a script (script1) which needs to call another script (script2).
    It should not wait for the answer, so script2 should run in the background.

    I really need to call the php script using:
    http://urlof myscript/script2.php

    I don't want to use it as a cgi and use CLI.

    I did a test with fsockopen and curl and a timeout of 10 seconds and call
    http://urlof myscript/script1.php from my browser:
    Script1 stops after 10 seconds => OK
    Script2 has been called by script1 and continue to run in the background
    =>OK
    After 60 seconds Script2 is killed by php!! => KO :(

    The script is working fine if i call http://urlof myscript/script2.php from
    my browser. It needs around 120 seconds to finish.

    Why is it stopping after 60 seconds when I use fsockopen and call it from
    script1?

    Here is the script in SCRIPT1:
    $fp = fsockopen("$hos t", 80, $errno, $errstr, 30);
    stream_set_time out($fp,10);
    if (!$fp) {
    echo "$errstr ($errno)<br />\n";
    } else {
    $out = "GET $path HTTP/1.1\r\n";
    $out .= "Host: $host\r\n";
    $out .= "Connection : Close\r\n\r\n";

    fwrite($fp, $out);
    $status = socket_get_stat us($fp);

    while (!feof($fp) && !$status['timed_out']) {
    echo fgets($fp, 128);
    $status = socket_get_stat us($fp);
    }
    fclose($fp);
    }


    I have added this in SCRIPT2:
    ignore_user_abo rt(FALSE); // becareful, FALSE really means it should ignore
    user abort !!!
    set_time_limit (120);
    ini_set("max_in put_time",120);
    ini_set("max_ex ecution_time",1 20);


    Do you have any idea??

    Thanks you for your help

    Rod


  • News Me

    #2
    Re: run a php in the background using http

    WebRod wrote:[color=blue]
    > Hi,
    >
    > i have a script (script1) which needs to call another script (script2).
    > It should not wait for the answer, so script2 should run in the background.
    >
    > I really need to call the php script using:
    > http://urlof myscript/script2.php
    >
    > I don't want to use it as a cgi and use CLI.
    >
    > I did a test with fsockopen and curl and a timeout of 10 seconds and call
    > http://urlof myscript/script1.php from my browser:
    > Script1 stops after 10 seconds => OK
    > Script2 has been called by script1 and continue to run in the background
    > =>OK
    > After 60 seconds Script2 is killed by php!! => KO :(
    >
    > The script is working fine if i call http://urlof myscript/script2.php from
    > my browser. It needs around 120 seconds to finish.
    >
    > Why is it stopping after 60 seconds when I use fsockopen and call it from
    > script1?
    >
    > Here is the script in SCRIPT1:
    > $fp = fsockopen("$hos t", 80, $errno, $errstr, 30);
    > stream_set_time out($fp,10);
    > if (!$fp) {
    > echo "$errstr ($errno)<br />\n";
    > } else {
    > $out = "GET $path HTTP/1.1\r\n";
    > $out .= "Host: $host\r\n";
    > $out .= "Connection : Close\r\n\r\n";
    >
    > fwrite($fp, $out);
    > $status = socket_get_stat us($fp);
    >
    > while (!feof($fp) && !$status['timed_out']) {
    > echo fgets($fp, 128);
    > $status = socket_get_stat us($fp);
    > }
    > fclose($fp);
    > }
    >
    >
    > I have added this in SCRIPT2:
    > ignore_user_abo rt(FALSE); // becareful, FALSE really means it should ignore
    > user abort !!!
    > set_time_limit (120);
    > ini_set("max_in put_time",120);
    > ini_set("max_ex ecution_time",1 20);
    >
    >
    > Do you have any idea??
    >
    > Thanks you for your help
    >
    > Rod
    >
    >[/color]

    Why not exec() it?

    exec("nohup /path/to/php /path/to/script2.php &");

    I used something similar to launch mpg123 when I was playing around with
    a web-based media player.

    NM

    --
    convert UPPERCASE NUMBER to a numeral to reply

    Comment

    • Chung Leong

      #3
      Re: run a php in the background using http


      "WebRod" <nomail@bouygte l.fr> wrote in message
      news:42090f71$0 $26205$7a628cd7 @news.club-internet.fr...[color=blue]
      > Hi,
      >
      > i have a script (script1) which needs to call another script (script2).
      > It should not wait for the answer, so script2 should run in the[/color]
      background.[color=blue]
      >
      > I really need to call the php script using:
      > http://urlof myscript/script2.php
      >
      > I don't want to use it as a cgi and use CLI.
      >
      > I did a test with fsockopen and curl and a timeout of 10 seconds and call
      > http://urlof myscript/script1.php from my browser:
      > Script1 stops after 10 seconds => OK
      > Script2 has been called by script1 and continue to run in the background
      > =>OK
      > After 60 seconds Script2 is killed by php!! => KO :(
      >
      > The script is working fine if i call http://urlof myscript/script2.php[/color]
      from[color=blue]
      > my browser. It needs around 120 seconds to finish.
      >
      > Why is it stopping after 60 seconds when I use fsockopen and call it from
      > script1?
      >
      > Here is the script in SCRIPT1:
      > $fp = fsockopen("$hos t", 80, $errno, $errstr, 30);
      > stream_set_time out($fp,10);
      > if (!$fp) {
      > echo "$errstr ($errno)<br />\n";
      > } else {
      > $out = "GET $path HTTP/1.1\r\n";
      > $out .= "Host: $host\r\n";
      > $out .= "Connection : Close\r\n\r\n";
      >
      > fwrite($fp, $out);
      > $status = socket_get_stat us($fp);
      >
      > while (!feof($fp) && !$status['timed_out']) {
      > echo fgets($fp, 128);
      > $status = socket_get_stat us($fp);
      > }
      > fclose($fp);
      > }
      >
      >
      > I have added this in SCRIPT2:
      > ignore_user_abo rt(FALSE); // becareful, FALSE really means it should[/color]
      ignore[color=blue]
      > user abort !!!
      > set_time_limit (120);
      > ini_set("max_in put_time",120);
      > ini_set("max_ex ecution_time",1 20);
      >
      >
      > Do you have any idea??
      >
      > Thanks you for your help
      >
      > Rod
      >
      >[/color]

      It would help if you use ignore_user_abo rt() correctly.
      ignore_user_abo rt(true) means ignore user abort.


      Comment

      • Colin McKinnon

        #4
        Re: run a php in the background using http

        News Me wrote:
        [color=blue]
        > WebRod wrote:[color=green]
        >> Hi,
        >>
        >> i have a script (script1) which needs to call another script (script2).
        >> It should not wait for the answer, so script2 should run in the
        >> background.
        >>[/color]
        >
        > Why not exec() it?
        >
        > exec("nohup /path/to/php /path/to/script2.php &");
        >
        > I used something similar to launch mpg123 when I was playing around with
        > a web-based media player.
        >[/color]

        'cos:
        1) the OP said he didn't want to run it from the CLI
        2) it's part of the same process group so the original script will hang
        around while it's running (hint: use 'at now ...' instead of '...&'

        C.

        Comment

        • WebRod

          #5
          Re: run a php in the background using http

          > It would help if you use ignore_user_abo rt() correctly.[color=blue]
          > ignore_user_abo rt(true) means ignore user abort.[/color]

          actually with windows xp sp2 and php 4.3.10, it's the opposite.
          ignore_user_abo rt(false) means ignore user abort.

          with linux
          ignore_user_abo rt(true) means ignore user abort.


          Rod



          Comment

          • NC

            #6
            Re: run a php in the background using http

            WebRod wrote:[color=blue]
            >
            > i have a script (script1) which needs to call another script[/color]
            (script2).[color=blue]
            > It should not wait for the answer, so script2 should run in the[/color]
            background.[color=blue]
            >
            > I really need to call the php script using:
            > http://urlof myscript/script2.php
            >
            > I don't want to use it as a cgi and use CLI.[/color]

            OK, then use wget and fork its execution into background.
            [color=blue]
            > Why is it stopping after 60 seconds when I use fsockopen
            > and call it from script1?[/color]

            Perhaps your system has an execution time limit not overridable
            from the scripts?

            Cheers,
            NC

            Comment

            • Chung Leong

              #7
              Re: run a php in the background using http


              "WebRod" <nomail@bouygte l.fr> wrote in message
              news:4209f575$0 $5212$7a628cd7@ news.club-internet.fr...[color=blue][color=green]
              > > It would help if you use ignore_user_abo rt() correctly.
              > > ignore_user_abo rt(true) means ignore user abort.[/color]
              >
              > actually with windows xp sp2 and php 4.3.10, it's the opposite.
              > ignore_user_abo rt(false) means ignore user abort.
              >
              > with linux
              > ignore_user_abo rt(true) means ignore user abort.[/color]

              I don't know where you got your info from, pal, but it is wrong.

              main.c, line 1759

              /* {{{ php_handle_abor ted_connection
              */
              PHPAPI void php_handle_abor ted_connection( void)
              {
              TSRMLS_FETCH();

              PG(connection_s tatus) = PHP_CONNECTION_ ABORTED;
              php_output_set_ status(0 TSRMLS_CC);

              if (!PG(ignore_use r_abort)) {
              zend_bailout();
              }
              }
              /* }}} */



              Comment

              • News Me

                #8
                Re: run a php in the background using http

                Colin McKinnon wrote:[color=blue]
                > News Me wrote:
                >
                >[color=green]
                >>WebRod wrote:
                >>[color=darkred]
                >>>Hi,
                >>>
                >>>i have a script (script1) which needs to call another script (script2).
                >>>It should not wait for the answer, so script2 should run in the
                >>>background .
                >>>[/color]
                >>
                >>Why not exec() it?
                >>
                >> exec("nohup /path/to/php /path/to/script2.php &");
                >>
                >>I used something similar to launch mpg123 when I was playing around with
                >>a web-based media player.
                >>[/color]
                >
                >
                > 'cos:
                > 1) the OP said he didn't want to run it from the CLI
                > 2) it's part of the same process group so the original script will hang
                > around while it's running (hint: use 'at now ...' instead of '...&'[/color]

                1) The OP is not running it from the command line, the webserver is.
                2) When I ran the script on a Fedora Core System, it didn't hang.

                NM

                --
                convert UPPERCASE NUMBER to a numeral to reply

                Comment

                • WebRod

                  #9
                  Re: run a php in the background using http

                  > I don't know where you got your info from, pal, but it is wrong.

                  Actually i did a small test to ckeck that.
                  The same test (script 1 is calling script2 using fsockopen and a small
                  timeout):

                  ignore_user_abo rt(true)
                  - windows => script2 stops WHEN script 1 stops (!!)
                  - linux => script2 will stop only at the end. (as expected)

                  ignore_user_abo rt(false)
                  - windows => script2 will stop only at the end. (!!)
                  - linux => script2 stops WHEN script 1 stops (as expected)

                  Maybe i have a corrupted version of php??
                  I have windows XP SP2, php 4.3.10

                  Rod


                  Comment

                  • WebRod

                    #10
                    Re: run a php in the background using http

                    [color=blue]
                    > 1) The OP is not running it from the command line, the webserver is.[/color]
                    Actually it's the same thing.
                    I explain you why I don't want to use it. My web server is on OVH.COM.
                    My script running from the CLI (from telnet or exec) use too many
                    ressources (!!) on the server and they kill it and send me an email to stop
                    this script or they will disable my website!!
                    The same script (I just change the extension from .cg to .ph and remove the
                    first line which specify the php path) is running very well, they don't kill
                    it, no problem!!

                    They don't explain me why there is a difference, so i don't want to use the
                    CLI anymore with them.



                    Comment

                    • WebRod

                      #11
                      Re: run a php in the background using http

                      Hi to all,

                      I have solved the problem.
                      The buffer was full because my script (script2) was displaying some output.

                      When the buffer is full, php stops the execution of the script!
                      I guess it happens because script1 (the caller) is already stopped

                      So i have removed all the "echo" and now it works like a charm!!!!

                      Rod


                      Comment

                      • Chung Leong

                        #12
                        Re: run a php in the background using http

                        "WebRod" <nomail@bouygte l.fr> wrote in message
                        news:420b1805$0 $5222$7a628cd7@ news.club-internet.fr...[color=blue][color=green]
                        > > I don't know where you got your info from, pal, but it is wrong.[/color]
                        >
                        > Actually i did a small test to ckeck that.
                        > The same test (script 1 is calling script2 using fsockopen and a small
                        > timeout):
                        >
                        > ignore_user_abo rt(true)
                        > - windows => script2 stops WHEN script 1 stops (!!)
                        > - linux => script2 will stop only at the end. (as expected)
                        >
                        > ignore_user_abo rt(false)
                        > - windows => script2 will stop only at the end. (!!)
                        > - linux => script2 stops WHEN script 1 stops (as expected)
                        >
                        > Maybe i have a corrupted version of php??
                        > I have windows XP SP2, php 4.3.10
                        >
                        > Rod
                        >[/color]

                        Your test was probably faulty. PHP only knows a connection is broken when it
                        tries to output data to the network socket. A snippet like the following
                        will run to the end despite a user abort since there isn't enough data to
                        overfill the default 4096 byte buffer:

                        for($i = 0; $i < 120; $i++) {
                        echo "Hello";
                        sleep(1);
                        }


                        Comment

                        • WebRod

                          #13
                          Re: run a php in the background using http

                          > Your test was probably faulty.

                          I don't think so
                          [color=blue]
                          > PHP only knows a connection is broken when it
                          > tries to output data to the network socket.[/color]

                          You're absoluty right, i know that point.
                          [color=blue]
                          >A snippet like the following[/color]
                          [..]

                          You're right, in that case could you provide a "right" script?

                          What is sure is that script2 (the real one) returns a lot of information.
                          (1) If I have ignore_user_abo rt(true), then script2 stops WHEN script 1
                          stops.
                          (2) If I have ignore_user_abo rt(false), then script2 will sotp at the end

                          What is the difference between (1) and (2)?
                          The flag and the duration of the script. (the content of the script is the
                          same, if it was a problem of empty buffer as you said, (1) and (2) will both
                          stop only at the end of the script)

                          But as i said it could only be a problem with [php 4.3.10/xp sp2] or a
                          specific problem on my machine (??)

                          Rod



                          Comment

                          Working...