SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can I run a php script as a background task?

    I'm mucking around with a script that sources information from an external web site service (the Australia Post Delivery Rate Calculator to be more specific).

    Sometimes this external service can be sluggish to respond, but I don't want this sluggishness to affect the browser response time of my web site.

    Is it therefore possible for a php script ('A') to kick-off another php script ('B') which will take the extra time to get the info it needs (ie. will run as a background task on the server), writes the results to a MySQL database, and then script A can pick that info up later on?

    Looking forward to your feedback...
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise

  2. #2
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It might be a little easier to answer that question if you were to provide a real-world example of what you are trying to accomplish.

  3. #3
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your feedback.

    I take your point about giving a real world example, but sourcing information from a site or service external to your own is (I think) a reasonably generic concept.

    In this particular example I am getting postal rates from an external source, but it could be one of a thousand different 'web service' type applications. So the solution I am looking for (if it exists) would be a generic solution that is in no way dependent on the type of external service I choose to use.

    The bottom line as I see it is: can I kick off a php script (running on a Linux+Apache platform) that goes away and does it's own thing independently of any user-browser factors? I don't want to hold up the user's movement around a web site while my background script goes about it's business.
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise

  4. #4
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm not sure this helps,
    but reading from the manual about the system function I see:

    Note: If you start a program using this function and want to leave it running in the background, you have to make sure that the output of that program is redirected to a file or some other output stream or else PHP will hang until the execution of the program ends.
    That said, from the version 4.3 of php a CLI ( command line) will be available ( now its experimental ).

    So maybe it's possible to launch something like

    PHP Code:
    system"php /path/to/file.php > dummy.txt" ); 
    ?!?

    I have no title to give any suggestions about this argument
    and for sure I'm saying some ****,
    but I thought that it could give you some ideas.



    p.s.
    Some references:
    http://www.php.net/manual/en/function.system.php
    and
    http://www.php.net/manual/en/features.commandline.php
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another way to approach this might be to use a cron job to run a file that will grab the info you need and store it in a MySQL database. running a cron job is independent of any client connections. if you do a search, i have responded on a thread how to set it up, and there are other threads that discuss it as well.

    as far as having the task go into the background when a client initiates it....i am not sure how/if that can be done. doing a cron job is my recommendation.

  6. #6
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might also be interested in this thread on "forking" a process in PHP.

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could do
    PHP Code:
    system"php /path/to/file.php > dummy.txt &" ); 
    the apersand (&) is used to place things in the background in the console, and works with the exec() type functions as well ( unix based systems only )

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also, the pcntl_fork() funciton mentioned in the other thread on fork()ing is now in PHP 4.1+

  9. #9
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all. Now I've got some options to play with.
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise

  10. #10
    The short answer is yes... Herbster's Avatar
    Join Date
    Oct 2001
    Location
    Bay City, Oregon
    Posts
    715
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Download phpAdsNew and look at the fakecron.php file. It "fakes" cron by running a background process triggered by a 1x1 pixel image placed on the page you want to trigger the background operation.

    Posting their code wouldn't be cool, so download and take a look. Easy to hack.

    http://sourceforge.net/projects/phpadsnew/

  11. #11
    SitePoint Zealot marcoBR's Avatar
    Join Date
    Jun 2002
    Location
    Brazil
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another article covering "Fork":

    http://www.phpfreaks.com/tutorials/71/0.php

  12. #12
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cron + curl = background
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.


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
  •