问题
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.
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
Line 10 is the line where shell_exec()
is. The question how to fix this issue? Why is this happening?
回答1:
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");
This is Unix shell syntax, I don't know if there's anything equivalent in Windows cmd.
来源:https://stackoverflow.com/questions/43693742/two-commands-successfully-run-manually-but-fail-when-run-in-shell-exec-giving