Can a PHP script run another PHP script and exit?

How does a PHP script run another PHP script and then exit, leaving another script running?

Also, is there a way for the second script to tell the PHP script when it reaches a certain line?

+6
scripting asynchronous php webserver
source share
6 answers

Here's how to do it. You tell the browser to read the first N characters of the output and then close the connection while your script continues to run until it is executed.

<?php ob_end_clean(); header("Connection: close"); ignore_user_abort(); // optional ob_start(); echo ('Text the user will see'); $size = ob_get_length(); header("Content-Length: $size"); ob_end_flush(); // Will not work flush(); // Unless both are called ! // At this point, the browser has closed connection to the web server // Do processing here include('other_script.php'); echo('Text user will never see'); ?> 
+7
source share

You can effectively achieve this forking and then call include or require .

parent.php:

 <?php $pid = pcntl_fork(); if ($pid == -1) { die("couldn't fork"); } else if ($pid) { // parent script echo "Parent waiting at " . date("H:i:s") . "\n"; pcntl_wait($status); echo "Parent done at " . date("H:i:s") . "\n"; } else { // child script echo "Sleeper started at " . date("H:i:s") . "\n"; include('sleeper.php'); echo "Sleeper done at " . date("H:i:s") . "\n"; } ?> 

sleeper.php:

 <?php sleep(3); ?> 

Output:

  $ php parent.php
 Sleeper started at 01:22:02
 Parent waiting at 01:22:02
 Sleeper done at 01:22:05
 Parent done at 01:22:05

However, forking essentially does not allow interactions between processes, so you will need to find another way to tell the parent that the child has reached a certain line, for example, you asked a question.

+3
source share

Here's a shot in the dark: you can try using the php OS runtime functions with & .

 exec("./somescript.php &"); 

Alternatively, if this does not work, you can try

 exec("nohup ./somescript.php &"); 

Edit: nohup is a POSIX command to ignore the HUP (hangup) signal, allowing the command to continue working after the user who issues the command has started the log out. The HUP (hang) signal is consistent with how the terminal warns of dependent logout processes.

+1
source share

Will pcntl_fork() do something similar to what you end up trying to accomplish? http://www.php.net/manual/en/function.pcntl-fork.php

+1
source share

If you do not want to create the pcntl extension, a good alternative is to use proc_open ().

http://www.php.net/manual/en/function.proc-open.php

Use this with stream_select () so that your PHP process can sleep until something happens with the child process you created.

This will effectively create the process in the background without blocking the parent PHP process. You, PHP, can read and write in STDIN, STDOUT, STDERR.

To complete the browser download (stop the download progress indicator), you can use what Milan Babushkov mentioned.

The key for the browser to consider that the HTTP request is complete is to send it the length of the content. To do this, you can start buffering the request and then clear it after sending the Content-Length header.

eg:

 <?php ob_start(); // render the HTML page and/or process stuff header('Content-Length: '.ob_get_length()); ob_flush(); flush(); // can do more processing ?> 
0
source share

You can create a request and close the connection immediately after writing it.

Check out the code http://drupal.org/project/httprl how this can be done (non-blocking request). I plan to push this library to github as soon as I get it more polished; something that can be run outside drupal. This should do what you are looking for.

0
source share

All Articles