Home » Php » http – Can a PHP script start another PHP script and exit?

http – Can a PHP script start another PHP script and exit?

Posted by: admin July 12, 2020 Leave a comment


How can a PHP script start another PHP script, and then exit, leaving the other script running?

Also, is there any way for the 2nd script to inform the PHP script when it reaches a particular line?

How to&Answers:

Here’s how to do it. You tell the browser to read in the first N characters of output and then close the connection, while your script keeps running until it’s done.

header("Connection: close");
ignore_user_abort(); // optional
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

echo('Text user will never see');


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



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





$ 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 does not inherently allow any inter-process communication, so you’d have to find some other way to inform the parent that the child has reached the specific line, like you asked in the question.


Here’s a shot in the dark: you could try using php’s OS execution functions with &.

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

Additionally, if that doesn’t work, you can try

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

Edit: nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. The HUP (hangup) signal is by convention the way a terminal warns depending processes of logout.


Would pcntl_fork() do something similar to what you’re ultimately trying to accomplish? http://www.php.net/manual/en/function.pcntl-fork.php


If you don’t want to build the pcntl extension, then a good alternative is to use proc_open().


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

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

To make the browser complete loading (stop the load progress indicator) then you can use what Milan Babuškov mentioned.

The key to making the browser think the HTTP request is complete, is to send it the content length. To do this you can start buffering the request, then flush it after you send the Content-Length header.




// render the HTML page and/or process stuff

header('Content-Length: '.ob_get_length());

// can do more processing



You can create a request and close the connection right after it is done being written to.

Checkout the code in http://drupal.org/project/httprl as can do this (non-blocking request). I plan on pushing this lib to github once I get it more polished; something that can be ran outside of drupal. This should do what your looking for.