SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Launch External PHP Script with shell_exec?

    I'm thinking of using shell_exec to launch an external php script (that talks with a remote mysql database) so the script in that external PHP file does not hold up my page from loading.

    Does this work? Does the PHP run shell_exec and just move on with the rest of the page/coding? Or does it wait for whatever external script is run to finish?

    And, if it works like I hope, does running shell_exec take more resources from the server than just running the script in the page?

    All info appreciated
    Cheers
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  2. #2
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    shell_exec waits for the output to be returned, therefore waits for the command to finish execution.

    If you do not want that, then simply discard the output by adding the following to the end of your command (inside shell_exec): > /dev/null 2>/dev/null &

    Example:

    PHP Code:
    shell_exec ('php external-file.php > /dev/null 2>/dev/null &'); 
    This will start running the external-file.php and forgets about it, because it doesn't expect back any output from the script.

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is awesome, thanks. Now, is there any disadvantage of using shell_exec over just running the script in the same page? The shell_exec script will likely be run over one-million times per day, so need to be sure. Also, you know of any way of having PHP not wait for a command to execute (finish) without doing it this way?

    Cheers!
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    only way i can think of is if your server supports pcntl_fork....
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •