在后台运行exec(scp)时没有发现速度差异

问题描述 投票:0回答:1

我正在尝试异步复制 10 个 70 MB 视频文件。

exec("scp -o StrictHostKeyChecking=accept-new -i /var/keys/devDevices_rsa MarTianez1.mp4 [email protected]:/tmp/test1 2>&1 > out.log", $output, $exitCode);
...
exec("scp -o StrictHostKeyChecking=accept-new -i /var/keys/devDevices_rsa MarTianez10.mp4 [email protected]:/tmp/test10 2>&1 > out.log", $output, $exitCode);

但是如果我从

2>&1 > out.log
命令中删除
scp
,我看不到任何时间差异。

PHP 8.1、Apache2、Ubuntu 22

php asynchronous exec
1个回答
0
投票

重定向的顺序很重要。

command 2>&1 > out.log
仅将标准输出重定向到文件。发生这种情况是因为在 stdout 重定向到文件之前 stderr 被重定向到 stdout。正确的顺序是
command > out.log 2>&1
(与
command &> out.log
相同。

此外,要强制进程在后台启动,您可以在命令末尾添加

&

异步运行代码:

exec("scp ... &> out.log &", $output, $exitCode);
© www.soinside.com 2019 - 2024. All rights reserved.