两条命令手动成功运行,但在shell_exec()中运行时失败,出现超时错误 [英] Two commands successfully run manually but fail when run in shell_exec(), giving TimeOut error

查看:352
本文介绍了两条命令手动成功运行,但在shell_exec()中运行时失败,出现超时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP shell_exec()同时执行两个dmd命令。一条命令运行Tshark 5秒钟。第二个命令运行 .exe 文件。 Tshark是一个程序,捕获通过网络接口传输的网络包。第二个程序(名为mtu.exe)将网络数据包从本地计算机发送到远程计算机。

I am trying to execute two dmd commands simultaneously using PHP shell_exec(). One command runs Tshark for 5 seconds. The second command runs an .exe file. Tshark is a program which captures network packs transferring over a network interface. The second program (named mtu.exe) sends network packets from my local machine to a remote machine.

问题是,当我手动运行这些命令时,我运行了第一个命令。过了一会儿,我运行第二个,一切顺利,在一秒钟左右,期望的数据包由 mtu.exe 传输并由<$捕获c $ c> tshark.exe 。一切正常。

Thing is that when I run these commands manually, I run the first command. A moment after that, I run the second one, and all goes well and within a second or so, the expected packets are transferred by mtu.exe and are captured by tshark.exe. Everything works perfectly well.

但是当我运行以下脚本来执行这些命令时,将得到以下输出:

But then when I run the following script to execute these commands, I get the following output:

$firstCommand = '"C:\Program Files\Wireshark\tshark.exe"  -a duration:5  -w capture.pcapng  2>&1';
echo $firstCommand."<br><br>";

$secondCommand = "mtu.exe -d0 -a43020008 -g43010008 -i987654321 -s"Merry Xmass"  2>&1";
echo $secondCommand."<br><br>";

echo shell_exec($firstCommand . " && " . $secondCommand);

输出:

Output:

"C:\Program Files\Wireshark\tshark.exe"  -a duration:5  -w capture.pcapng  2>&1

mtu.exe -d0 -a43020008 -g43010008 -i987654321 -s"Merry Xmass"  2>&1

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\Test\index.php on line 10

第10行是 shell_exec()所在的行。问题如何解决此问题?为什么会发生这种情况?

Line 10 is the line where shell_exec() is. The question how to fix this issue? Why is this happening?

推荐答案

在后台运行 tshark 并使用& ,因此外壳程序无需等待它完成就可以运行 mtu 。然后使用 wait 命令等待后台命令完成。

Run tshark in the background with & so the shell doesn't wait for it to finish before running mtu. Then use the wait command to wait for the background command to finish.

echo shell_exec($firstCommand . " & " . $secondCommand . "; wait");

这是Unix shell语法,我不知道Windows cmd中是否有等效的东西。

This is Unix shell syntax, I don't know if there's anything equivalent in Windows cmd.

这篇关于两条命令手动成功运行,但在shell_exec()中运行时失败,出现超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆