php execute a background process

I need to execute a directory copy upon a user action, but the directories are quite large, so I would like to be able to perform such an action without the user being aware of the time it takes for the copy to complete.

Any suggestions would be much appreciated.


Assuming this is running on a Linux machine, I've always handled it like this:

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

This launches the command $cmd , redirects the command output to $outputfile , and writes the process id to $pidfile .

That lets you easily monitor what the process is doing and if it's still running.

function isRunning($pid){
    try{
        $result = shell_exec(sprintf("ps %d", $pid));
        if( count(preg_split("/n/", $result)) > 2){
            return true;
        }
    }catch(Exception $e){}

    return false;
}

Write the process as a server-side script in whatever language (php/bash/perl/etc) is handy and then call it from the process control functions in your php script.

The function probably detects if standard io is used as the output stream and if it is then that will set the return value..if not then it ends

Proc_Close (Proc_Open ("./command --foo=1 &", Array (), $foo));

I tested this quickly from the command line using "sleep 25s" as the command and it worked like a charm.

(Answer found here)


I'd just like to add a very simple example for testing this functionality on Windows:

Create the following two files and save them to a web directory:

foreground.php:

<?php

ini_set("display_errors",1);
error_reporting(E_ALL);

echo "<pre>loading page</pre>";

function run_background_process()
{
    file_put_contents("testprocesses.php","foreground start time = " . time() . "n");
    echo "<pre>  foreground start time = " . time() . "</pre>";

    // output from the command must be redirected to a file or another output stream 
    // http://ca.php.net/manual/en/function.exec.php

    exec("php background.php > testoutput.php 2>&1 & echo $!", $output);

    echo "<pre>  foreground end time = " . time() . "</pre>";
    file_put_contents("testprocesses.php","foreground end time = " . time() . "n", FILE_APPEND);
    return $output;
}

echo "<pre>calling run_background_process</pre>";

$output = run_background_process();

echo "<pre>output = "; print_r($output); echo "</pre>";
echo "<pre>end of page</pre>";
?>

background.php:

<?
file_put_contents("testprocesses.php","background start time = " . time() . "n", FILE_APPEND);
sleep(10);
file_put_contents("testprocesses.php","background end time = " . time() . "n", FILE_APPEND);
?>

Give IUSR permission to write to the directory in which you created the above files

Give IUSR permission to READ and EXECUTE C:WindowsSystem32cmd.exe

Hit foreground.php from a web browser

The following should be rendered to the browser w/the current timestamps and local resource # in the output array:

loading page
calling run_background_process
  foreground start time = 1266003600
  foreground end time = 1266003600
output = Array
(
    [0] => 15010
)
end of page

You should see testoutput.php in the same directory as the above files were saved, and it should be empty

You should see testprocesses.php in the same directory as the above files were saved, and it should contain the following text w/the current timestamps:

foreground start time = 1266003600
foreground end time = 1266003600
background start time = 1266003600
background end time = 1266003610
链接地址: http://www.djcxy.com/p/37160.html

上一篇: Xdebug的

下一篇: php执行后台进程