Launch a background task from a PHP script?

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

    Launch a background task from a PHP script?

    Any idea how I can launch a background task from a PHP script?

    For example, when a user posts on my message board, it may fire many
    e-mail notifications to other users, and other tasks. I want the
    posting confirmation page to end displaying quickly, without waiting
    for those notifications or other tasks to complete.

    So basically what I want is to launch 'background.php ' from 'main.php',
    with the user seeing only the output of - and waiting only for the end
    of - 'main.php'. Without the need to wait for the end of the possibly
    long 'background.php ', like if I had simply included it in 'main.php'.

    According to the PHP Manual, the function register_shutdo wn_function()
    was the solution "in PHP 4.0.6 and earlier under Apache", but "since
    PHP 4.1 the shutdown functions are called as the part of the request"
    so that PHP waits until the shutdown functions are completed before
    closing the connection to the user.

    E.g. in PHP 4.1+ this

    <?php
    function background () {
    echo 'Starting background task... ';
    // Do something long that I don't want the user to see or wait for
    sleep(5);
    echo 'End of background task. ';
    }

    echo 'Starting main script... ';
    register_shutdo wn_function('ba ckground');
    echo 'End of main script. ';
    ?>

    will output:

    Starting main script... End of main script. Starting background task...
    End of background task.

    I want a way to output only:

    Starting main script... End of main script.

    and do the remaining in background.

  • Michael Austin

    #2
    Re: Launch a background task from a PHP script?

    Francois Bonzon wrote:
    [color=blue]
    > Any idea how I can launch a background task from a PHP script?
    >
    > For example, when a user posts on my message board, it may fire many
    > e-mail notifications to other users, and other tasks. I want the posting
    > confirmation page to end displaying quickly, without waiting for those
    > notifications or other tasks to complete.
    >
    > So basically what I want is to launch 'background.php ' from 'main.php',
    > with the user seeing only the output of - and waiting only for the end
    > of - 'main.php'. Without the need to wait for the end of the possibly
    > long 'background.php ', like if I had simply included it in 'main.php'.
    >
    > According to the PHP Manual, the function register_shutdo wn_function()
    > was the solution "in PHP 4.0.6 and earlier under Apache", but "since PHP
    > 4.1 the shutdown functions are called as the part of the request" so
    > that PHP waits until the shutdown functions are completed before closing
    > the connection to the user.
    >
    > E.g. in PHP 4.1+ this
    >
    > <?php
    > function background () {
    > echo 'Starting background task... ';
    > // Do something long that I don't want the user to see or wait for
    > sleep(5);
    > echo 'End of background task. ';
    > }
    >
    > echo 'Starting main script... ';
    > register_shutdo wn_function('ba ckground');
    > echo 'End of main script. ';
    > ?>
    >
    > will output:
    >
    > Starting main script... End of main script. Starting background task...
    > End of background task.
    >
    > I want a way to output only:
    >
    > Starting main script... End of main script.
    >
    > and do the remaining in background.
    >[/color]


    What platform are you on?

    if this is UNIX then you can do an exec() or system() call or something like
    `./executethisfile > out.log &`
    note these are back-ticks (key on the top row far left ~` )

    the "&" tells the shell script to execute in the background. Not sere how you
    would accomplish this on the Windows platform, but then again if you are doing
    anything serious, Windows is not your server platform.

    --
    Michael Austin.
    DBA Consultant
    Donations welcomed. Http://www.firstdbasource.com/donations.html
    :)

    Comment

    • Mladen Gogala

      #3
      Re: Launch a background task from a PHP script?

      On Mon, 20 Feb 2006 02:00:38 +0100, wrote:
      [color=blue]
      > Any idea how I can launch a background task from a PHP script?[/color]

      pcntl_fork will do that for you.

      --


      Comment

      • Mladen Gogala

        #4
        Re: Launch a background task from a PHP script?

        On Mon, 20 Feb 2006 02:44:13 +0000, Michael Austin wrote:
        [color=blue]
        > What platform are you on?
        >
        > if this is UNIX then you can do an exec() or system() call or something like
        > `./executethisfile > out.log &`
        > note these are back-ticks (key on the top row far left ~` )[/color]

        On the Unix-like operating systems, there is PCNTL extension which has
        calls like fork, exec, wait and signal. This extension is not available
        on Windows. On windows, there are .COM and .NOT functions in the manual.
        Not being a windows person, I don't really know whether those can be
        used.

        --


        Comment

        • d

          #5
          Re: Launch a background task from a PHP script?

          "Michael Austin" <maustin@firstd basource.com> wrote in message
          news:12aKf.3254 7$Jd.19077@news svr25.news.prod igy.net...[color=blue]
          > Francois Bonzon wrote:
          >[color=green]
          >> Any idea how I can launch a background task from a PHP script?
          >>
          >> For example, when a user posts on my message board, it may fire many
          >> e-mail notifications to other users, and other tasks. I want the posting
          >> confirmation page to end displaying quickly, without waiting for those
          >> notifications or other tasks to complete.
          >>
          >> So basically what I want is to launch 'background.php ' from 'main.php',
          >> with the user seeing only the output of - and waiting only for the end
          >> of - 'main.php'. Without the need to wait for the end of the possibly
          >> long 'background.php ', like if I had simply included it in 'main.php'.
          >>
          >> According to the PHP Manual, the function register_shutdo wn_function()
          >> was the solution "in PHP 4.0.6 and earlier under Apache", but "since PHP
          >> 4.1 the shutdown functions are called as the part of the request" so that
          >> PHP waits until the shutdown functions are completed before closing the
          >> connection to the user.
          >>
          >> E.g. in PHP 4.1+ this
          >>
          >> <?php
          >> function background () {
          >> echo 'Starting background task... ';
          >> // Do something long that I don't want the user to see or wait for
          >> sleep(5);
          >> echo 'End of background task. ';
          >> }
          >>
          >> echo 'Starting main script... ';
          >> register_shutdo wn_function('ba ckground');
          >> echo 'End of main script. ';
          >> ?>
          >>
          >> will output:
          >>
          >> Starting main script... End of main script. Starting background task...
          >> End of background task.
          >>
          >> I want a way to output only:
          >>
          >> Starting main script... End of main script.
          >>
          >> and do the remaining in background.
          >>[/color]
          >
          >
          > What platform are you on?
          >
          > if this is UNIX then you can do an exec() or system() call or something
          > like
          > `./executethisfile > out.log &`
          > note these are back-ticks (key on the top row far left ~` )
          >
          > the "&" tells the shell script to execute in the background. Not sere how
          > you would accomplish this on the Windows platform, but then again if you
          > are doing anything serious, Windows is not your server platform.[/color]

          Grow up :) There are plenty of serious things one can do on Windows, infact
          some one can only do on Windows.
          [color=blue]
          > --
          > Michael Austin.
          > DBA Consultant
          > Donations welcomed. Http://www.firstdbasource.com/donations.html
          > :)[/color]


          Comment

          • Michael Austin

            #6
            Re: Launch a background task from a PHP script?

            d wrote:
            [color=blue]
            > "Michael Austin" <maustin@firstd basource.com> wrote in message
            > news:12aKf.3254 7$Jd.19077@news svr25.news.prod igy.net...
            >[color=green]
            >>Francois Bonzon wrote:
            >>
            >>[color=darkred]
            >>>Any idea how I can launch a background task from a PHP script?
            >>>
            >>>For example, when a user posts on my message board, it may fire many
            >>>e-mail notifications to other users, and other tasks. I want the posting
            >>>confirmati on page to end displaying quickly, without waiting for those
            >>>notification s or other tasks to complete.
            >>>
            >>>So basically what I want is to launch 'background.php ' from 'main.php',
            >>>with the user seeing only the output of - and waiting only for the end
            >>>of - 'main.php'. Without the need to wait for the end of the possibly
            >>>long 'background.php ', like if I had simply included it in 'main.php'.
            >>>
            >>>According to the PHP Manual, the function register_shutdo wn_function()
            >>>was the solution "in PHP 4.0.6 and earlier under Apache", but "since PHP
            >>>4.1 the shutdown functions are called as the part of the request" so that
            >>>PHP waits until the shutdown functions are completed before closing the
            >>>connection to the user.
            >>>
            >>>E.g. in PHP 4.1+ this
            >>>
            >>><?php
            >>>function background () {
            >>> echo 'Starting background task... ';
            >>> // Do something long that I don't want the user to see or wait for
            >>> sleep(5);
            >>> echo 'End of background task. ';
            >>>}
            >>>
            >>>echo 'Starting main script... ';
            >>>register_shu tdown_function( 'background');
            >>>echo 'End of main script. ';
            >>>?>
            >>>
            >>>will output:
            >>>
            >>>Starting main script... End of main script. Starting background task...
            >>>End of background task.
            >>>
            >>>I want a way to output only:
            >>>
            >>>Starting main script... End of main script.
            >>>
            >>>and do the remaining in background.
            >>>[/color]
            >>
            >>
            >>What platform are you on?
            >>
            >>if this is UNIX then you can do an exec() or system() call or something
            >>like
            >>`./executethisfile > out.log &`
            >>note these are back-ticks (key on the top row far left ~` )
            >>
            >>the "&" tells the shell script to execute in the background. Not sere how
            >>you would accomplish this on the Windows platform, but then again if you
            >>are doing anything serious, Windows is not your server platform.[/color]
            >
            >
            > Grow up :) There are plenty of serious things one can do on Windows, infact
            > some one can only do on Windows.[/color]

            I have not found anything "serious" I can do on windows that I can't do on other
            platforms - and MY primary OS does not end in IX. :)


            --
            Michael Austin.
            DBA Consultant
            Donations welcomed. Http://www.firstdbasource.com/donations.html
            :)
            Long Live OpenVMS.

            Comment

            • Francois Bonzon

              #7
              Re: Launch a background task from a PHP script?

              On 2006-02-20 03:44:13 +0100, Michael Austin said:
              [color=blue]
              > Francois Bonzon wrote:
              >[color=green]
              >> Any idea how I can launch a background task from a PHP script?
              >>
              >> For example, when a user posts on my message board, it may fire many
              >> e-mail notifications to other users, and other tasks. I want the
              >> posting confirmation page to end displaying quickly, without waiting
              >> for those notifications or other tasks to complete.
              >>
              >> So basically what I want is to launch 'background.php ' from 'main.php',
              >> with the user seeing only the output of - and waiting only for the end
              >> of - 'main.php'. Without the need to wait for the end of the possibly
              >> long 'background.php ', like if I had simply included it in 'main.php'.
              >>
              >> According to the PHP Manual, the function register_shutdo wn_function()
              >> was the solution "in PHP 4.0.6 and earlier under Apache", but "since
              >> PHP 4.1 the shutdown functions are called as the part of the request"
              >> so that PHP waits until the shutdown functions are completed before
              >> closing the connection to the user.
              >>
              >> E.g. in PHP 4.1+ this
              >>
              >> <?php
              >> function background () {
              >> echo 'Starting background task... ';
              >> // Do something long that I don't want the user to see or wait for
              >> sleep(5);
              >> echo 'End of background task. ';
              >> }
              >>
              >> echo 'Starting main script... ';
              >> register_shutdo wn_function('ba ckground');
              >> echo 'End of main script. ';
              >> ?>
              >>
              >> will output:
              >>
              >> Starting main script... End of main script. Starting background task...
              >> End of background task.
              >>
              >> I want a way to output only:
              >>
              >> Starting main script... End of main script.
              >>
              >> and do the remaining in background.
              >>[/color]
              >
              >
              > What platform are you on?
              >
              > if this is UNIX then you can do an exec() or system() call or something like
              > `./executethisfile > out.log &`
              > note these are back-ticks (key on the top row far left ~` )
              >
              > the "&" tells the shell script to execute in the background. Not sere
              > how you would accomplish this on the Windows platform, but then again
              > if you are doing anything serious, Windows is not your server platform.[/color]

              I'm on a UNIX platform. I tested your solution, and it works fine. Thank you.

              However, some hosts where I plan to use this disabled the exec() and
              system() functions. The pcntl extension is also disabled there. So I
              guess it can't be helped here.

              Comment

              Working...