我需要在用户操作时执行目录复制,但目录相当大,因此我希望能够在用户不知道完成复制所需时间的情况下执行这样的操作。
任何建议都将不胜感激。
我需要在用户操作时执行目录复制,但目录相当大,因此我希望能够在用户不知道完成复制所需时间的情况下执行这样的操作。
任何建议都将不胜感激。
当前回答
我只想添加一个非常简单的例子来测试Windows上的这个功能:
创建以下两个文件,并将它们保存到web目录:
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);
?>
赋予IUSR写入创建上述文件的目录的权限
赋予IUSR读取和执行C:\Windows\System32\cmd.exe的权限
在浏览器中点击前台。php
下面的代码应该在输出数组中使用当前时间戳和本地资源#呈现给浏览器:
loading page
calling run_background_process
foreground start time = 1266003600
foreground end time = 1266003600
output = Array
(
[0] => 15010
)
end of page
您应该看到testoutput.php位于与上面文件保存的相同目录中,并且它应该为空
你应该看到testprocesses.php位于与上面文件相同的目录中,它应该包含以下带有当前时间戳的文本:
foreground start time = 1266003600
foreground end time = 1266003600
background start time = 1266003600
background end time = 1266003610
其他回答
我知道这是一个有100年历史的帖子,但不管怎样,我觉得它可能对某人有用。你可以在页面的某个地方放一个不可见的图像,指向需要在后台运行的url,就像这样:
<img src="run-in-background.php" border="0" alt="" width="1" height="1" />
我只想添加一个非常简单的例子来测试Windows上的这个功能:
创建以下两个文件,并将它们保存到web目录:
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);
?>
赋予IUSR写入创建上述文件的目录的权限
赋予IUSR读取和执行C:\Windows\System32\cmd.exe的权限
在浏览器中点击前台。php
下面的代码应该在输出数组中使用当前时间戳和本地资源#呈现给浏览器:
loading page
calling run_background_process
foreground start time = 1266003600
foreground end time = 1266003600
output = Array
(
[0] => 15010
)
end of page
您应该看到testoutput.php位于与上面文件保存的相同目录中,并且它应该为空
你应该看到testprocesses.php位于与上面文件相同的目录中,它应该包含以下带有当前时间戳的文本:
foreground start time = 1266003600
foreground end time = 1266003600
background start time = 1266003600
background end time = 1266003610
PHP脚本不同于其他桌面应用程序开发语言。在桌面应用程序语言中,我们可以设置守护线程来运行后台进程,但在PHP中,当用户请求一个页面时,进程就会发生。但是,可以使用php脚本运行的服务器的cron作业功能来设置后台作业。
用任何方便的语言(php/bash/perl/etc)将进程作为服务器端脚本编写,然后从php脚本中的进程控制函数调用它。
该函数可能会检测是否使用标准io作为输出流,如果是,则设置返回值,如果不是,则结束
proc_close( proc_open( "./command --foo=1 &", array(), $foo ) );
我在命令行中使用“sleep 25s”作为命令快速测试了这个功能,效果非常好。
(答案在这里)
您可能想尝试将此附加到您的命令中
>/dev/null 2>/dev/null &
eg.
shell_exec('service named reload >/dev/null 2>/dev/null &');