Can a PHP script start another PHP script and exit

2019-03-13 22:17发布

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?

6条回答
Fickle 薄情
2楼-- · 2019-03-13 22:52

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

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

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.

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

?>
查看更多
放我归山
3楼-- · 2019-03-13 23:04

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.

<?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');
?>
查看更多
相关推荐>>
4楼-- · 2019-03-13 23:07

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

查看更多
叼着烟拽天下
5楼-- · 2019-03-13 23:08

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.

查看更多
爷、活的狠高调
6楼-- · 2019-03-13 23:10

You can effectively achieve this by forking and then calling 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 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.

查看更多
在下西门庆
7楼-- · 2019-03-13 23:10

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.

查看更多
登录 后发表回答