2

在 LAMP 盒子上,我通过 PHP 的 exec 函数循环调用 SCP 复制函数,将多个图像复制到远程服务器:

exec("scp ".$this->GetUploadPath(true)." ".$currentServer->Scp.":".$this->GetServerPath($currentServer, true));

结果:工作正常,但 PHP 脚本需要几秒钟才能完成。

但是我不希望 PHP 脚本等待 SCP 作业完成,所以我尝试了以下方法之一:

exec("nohup scp ".$this->GetUploadPath(true)." ".$currentServer->Scp.":".$this->GetServerPath($currentServer, true) . " 1>/dev/null/ 2>&1 &");

结果:PHP 脚本要快得多,但 SCP 尚未完成。图像不会复制到远程服务器。

exec("nohup scp ".$this->GetUploadPath(true)." ".$currentServer->Scp.":".$this->GetServerPath($currentServer, true) . " &");

结果:图像被复制,但是 php 脚本运行时间没有改善,所以我猜 php 脚本仍然等待 SCP 例程完成,尽管我“后台”调用。

任何想法如何实现它,PHP脚本不等待,但SCP过程已经完成?

先感谢您!

4

1 回答 1

0

Exceeding the maximum simultaneous connection to the server could be the problem. So limit the simultaneous connections or simply upload it one by one. If waiting before the browser is your problem, you could add the following to your php script

ignore_user_abort(true);
set_time_limit(0);

and upload using looping the following

exec("scp ".$this->GetUploadPath(true)." ".$currentServer->Scp.":".$this->GetServerPath($currentServer, true) . " 1>/dev/null/ 2>&1");

after triggering the php script from your browser, you could even close browser, the above piece of code will do the magic for you.

于 2013-01-15T07:41:20.743 回答