php执行后台进程 [英] php execute a background process
本文介绍了php执行后台进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在执行用户操作时执行目录复制,但是目录很大,因此我希望能够执行这样的操作,而用户却不知道完成复制所花费的时间.
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.
任何建议将不胜感激.
推荐答案
假设此程序在Linux机器上运行,我总是这样处理:
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));
这将启动命令$cmd
,将命令输出重定向到$outputfile
,并将进程ID写入$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;
}
这篇关于php执行后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文