我需要在用户操作时执行目录复制,但目录相当大,因此我希望能够在用户不知道完成复制所需时间的情况下执行这样的操作。
任何建议都将不胜感激。
我需要在用户操作时执行目录复制,但目录相当大,因此我希望能够在用户不知道完成复制所需时间的情况下执行这样的操作。
任何建议都将不胜感激。
假设这是在Linux机器上运行的,我总是这样处理它:
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
这将启动命令$cmd,将命令输出重定向到$outputfile,并将进程id写入$pidfile。
这使您可以轻松地监视进程正在做什么以及它是否仍在运行。
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;
}
用任何方便的语言(php/bash/perl/etc)将进程作为服务器端脚本编写,然后从php脚本中的进程控制函数调用它。
该函数可能会检测是否使用标准io作为输出流,如果是,则设置返回值,如果不是,则结束
proc_close( proc_open( "./command --foo=1 &", array(), $foo ) );
我在命令行中使用“sleep 25s”作为命令快速测试了这个功能,效果非常好。
(答案在这里)
我只想添加一个非常简单的例子来测试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脚本,用wget命令“调用”。当然,与系统上的任何其他PHP脚本一样,这个后台PHP脚本将以特权执行。
下面是一个在Windows上使用gnuwin32包中的wget的例子。
后台代码(文件test-proc-bg.php)为例…
sleep(5); // some delay
file_put_contents('test.txt', date('Y-m-d/H:i:s.u')); // writes time in a file
前台脚本,调用…
$proc_command = "wget.exe http://localhost/test-proc-bg.php -q -O - -b";
$proc = popen($proc_command, "r");
pclose($proc);
你必须使用popen/pclose才能正常工作。
wget选项:
-q keeps wget quiet.
-O - outputs to stdout.
-b works on background
我知道这是一个有100年历史的帖子,但不管怎样,我觉得它可能对某人有用。你可以在页面的某个地方放一个不可见的图像,指向需要在后台运行的url,就像这样:
<img src="run-in-background.php" border="0" alt="" width="1" height="1" />
与其启动后台进程,不如创建一个触发器文件,并让像cron或autosys这样的调度程序定期执行一个寻找触发器文件并对其进行操作的脚本?触发器可以包含指令甚至原始命令(更好的方法是将其变成shell脚本)。
您可能想尝试将此附加到您的命令中
>/dev/null 2>/dev/null &
eg.
shell_exec('service named reload >/dev/null 2>/dev/null &');
您可以尝试像Resque这样的排队系统。然后,您可以生成一个作业,该作业处理信息并快速返回“处理”图像。使用这种方法,你不知道什么时候完成。
此解决方案适用于规模较大的应用程序,在这些应用程序中,您不希望前端机器承担繁重的工作,因此它们可以处理用户请求。 因此,它可能适用于文件和文件夹等物理数据,也可能不适用,但对于处理更复杂的逻辑或其他异步任务(如新的注册邮件),它是很好的,而且具有很强的可伸缩性。
如果使用PHP,使用pcntl_fork有一个更简单的方法:
http://www.php.net/manual/en/function.pcntl-fork.php
PHP脚本不同于其他桌面应用程序开发语言。在桌面应用程序语言中,我们可以设置守护线程来运行后台进程,但在PHP中,当用户请求一个页面时,进程就会发生。但是,可以使用php脚本运行的服务器的cron作业功能来设置后台作业。
我大量使用fast_cgi_finish_request()
结合使用闭包和register_shutdown_function()
$message ='job executed';
$backgroundJob = function() use ($message) {
//do some work here
echo $message;
}
然后注册这个闭包,在关闭之前执行。
register_shutdown_function($backgroundJob);
最后,当响应被发送到客户端时,您可以关闭到客户端的连接,并继续使用PHP进程:
fast_cgi_finish_request();
闭包将在fast_cgi_finish_request之后执行。
$消息在任何时候都不可见。您可以注册任意数量的闭包,但要注意脚本执行时间。 这将只在PHP作为一个快速CGI模块运行时工作(这是正确的吗?!)
对于我们这些使用Windows的人来说,看看这个:
参考:http://php.net/manual/en/function.exec.php # 43917
我也曾试图在后台运行一个程序 当脚本继续执行时,Windows。这种方法不像 其他解决方案允许您启动任何程序最小化,最大化, 或者根本就没有窗户。llbra@phpbrasil的解决方案确实有效,但它 有时会在桌面上产生一个不需要的窗口 希望隐藏任务运行。
在后台最小化启动Notepad.exe:
<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("notepad.exe", 7, false);
?>
在后台启动一个shell命令:
<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("cmd /C dir /S %windir%", 0, false);
?>
启动MSPaint maximized,等待你关闭它,然后继续脚本:
<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("mspaint.exe", 3, true);
?>
有关Run()方法的更多信息,请访问: http://msdn.microsoft.com/library/en-us/script56/html/wsMthRun.asp
编辑网址:
请访问https://technet.microsoft.com/en-us/library/ee156605.aspx,因为上面的链接已经不存在了。
下面是在PHP中启动后台进程的函数。在大量阅读和测试不同的方法和参数之后,终于创建了一个在Windows上也能工作的程序。
function LaunchBackgroundProcess($command){
// Run command Asynchroniously (in a separate thread)
if(PHP_OS=='WINNT' || PHP_OS=='WIN32' || PHP_OS=='Windows'){
// Windows
$command = 'start "" '. $command;
} else {
// Linux/UNIX
$command = $command .' /dev/null &';
}
$handle = popen($command, 'r');
if($handle!==false){
pclose($handle);
return true;
} else {
return false;
}
}
注意1:在windows上,不要像其他地方建议的那样使用/B参数。它强制进程运行与start命令本身相同的控制台窗口,导致进程被同步处理。要在单独的线程中(异步地)运行进程,请不要使用/B。
注2:如果命令是带引号的路径,则必须在start ""后加上空双引号。Start命令将第一个带引号的参数解释为窗口标题。
使用此函数在后台运行程序。它是跨平台的,完全可定制的。
<?php
function startBackgroundProcess(
$command,
$stdin = null,
$redirectStdout = null,
$redirectStderr = null,
$cwd = null,
$env = null,
$other_options = null
) {
$descriptorspec = array(
1 => is_string($redirectStdout) ? array('file', $redirectStdout, 'w') : array('pipe', 'w'),
2 => is_string($redirectStderr) ? array('file', $redirectStderr, 'w') : array('pipe', 'w'),
);
if (is_string($stdin)) {
$descriptorspec[0] = array('pipe', 'r');
}
$proc = proc_open($command, $descriptorspec, $pipes, $cwd, $env, $other_options);
if (!is_resource($proc)) {
throw new \Exception("Failed to start background process by command: $command");
}
if (is_string($stdin)) {
fwrite($pipes[0], $stdin);
fclose($pipes[0]);
}
if (!is_string($redirectStdout)) {
fclose($pipes[1]);
}
if (!is_string($redirectStderr)) {
fclose($pipes[2]);
}
return $proc;
}
注意,在命令启动后,默认情况下,该函数关闭正在运行进程的stdin和stdout。你可以通过$redirectStdout和$redirectStderr参数将进程输出重定向到某个文件中。
windows用户注意: 不能以以下方式将stdout/stderr重定向为null:
startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
然而,你可以这样做:
startBackgroundProcess('ping yandex.com >nul 2>&1');
*nix用户注意事项:
1)如果你想得到实际的PID,使用exec shell命令:
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
2)使用$stdin参数,如果你想传递一些数据到你的程序的输入:
startBackgroundProcess('cat > input.txt', "Hello world!\n");
一个适用于Windows和Linux的解决方案。在我的github页面上找到更多信息。
function run_process($cmd,$outputFile = '/dev/null', $append = false){
$pid=0;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {//'This is a server using Windows!';
$cmd = 'wmic process call create "'.$cmd.'" | find "ProcessId"';
$handle = popen("start /B ". $cmd, "r");
$read = fread($handle, 200); //Read the output
$pid=substr($read,strpos($read,'=')+1);
$pid=substr($pid,0,strpos($pid,';') );
$pid = (int)$pid;
pclose($handle); //Close
}else{
$pid = (int)shell_exec(sprintf('%s %s %s 2>&1 & echo $!', $cmd, ($append) ? '>>' : '>', $outputFile));
}
return $pid;
}
function is_process_running($pid){
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {//'This is a server using Windows!';
//tasklist /FI "PID eq 6480"
$result = shell_exec('tasklist /FI "PID eq '.$pid.'"' );
if (count(preg_split("/\n/", $result)) > 0 && !preg_match('/No tasks/', $result)) {
return true;
}
}else{
$result = shell_exec(sprintf('ps %d 2>&1', $pid));
if (count(preg_split("/\n/", $result)) > 2 && !preg_match('/ERROR: Process ID out of range/', $result)) {
return true;
}
}
return false;
}
function stop_process($pid){
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {//'This is a server using Windows!';
$result = shell_exec('taskkill /PID '.$pid );
if (count(preg_split("/\n/", $result)) > 0 && !preg_match('/No tasks/', $result)) {
return true;
}
}else{
$result = shell_exec(sprintf('kill %d 2>&1', $pid));
if (!preg_match('/No such process/', $result)) {
return true;
}
}
}
感谢这个答案:Symfony process Component是运行后台进程的一个完美工具,它基于proc_*函数,但是使用起来更容易。有关更多信息,请参阅其文档。
如果希望通过PHP执行后台进程,请将命令的输出输送到/dev/null,并在命令末尾添加&。
exec("bg_process > /dev/null &");
注意,不能使用exec()的$output参数,否则PHP将挂起(可能直到进程完成)。
老问题的新答案。使用这个库,下面的代码将生成一个异步/并行PHPThread来执行后台工作。
必须有pcntl, posix和套接字扩展名 在CLI模式下设计/测试。
EZ代码示例:
function threadproc($thread, $param) {
echo "\tI'm a PHPThread. In this example, I was given only one parameter: \"". print_r($param, true) ."\" to work with, but I can accept as many as you'd like!\n";
for ($i = 0; $i < 10; $i++) {
usleep(1000000);
echo "\tPHPThread working, very busy...\n";
}
return "I'm a return value!";
}
$thread_id = phpthread_create($thread, array(), "threadproc", null, array("123456"));
echo "I'm the main thread doing very important work!\n";
for ($n = 0; $n < 5; $n++) {
usleep(1000000);
echo "Main thread...working!\n";
}
echo "\nMain thread done working. Waiting on our PHPThread...\n";
phpthread_join($thread_id, $retval);
echo "\n\nOur PHPThread returned: " . print_r($retval, true) . "!\n";
来自PHP官方文档(php.net)
<?php
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
?>