0
votes

ADDENDUM: The problem in the question below is due to the SECOND command. When I comment out the second statement, the first one runs as expected and produces expected results. :s


I needed to run two terminal commands from PHP SIMULTANEOUSLY, and nothing was solving my problem (as you can see here and here, if you want to). The first command is supposed to run Tshark for 5 seconds and capture network traffic (packets). The second command was supposed to generate some network packets and send them. The whole idea is that the network packets produced by the second command should be captured by Tshark, started by the first command.

In other words I needed to start Tshark in the background, and without waiting for it to finish, I needed to run the second command.

Finally I read somewhere if we end a terminal command with an &, the command is run in the BACKGROUND, and we can continue running other commands in the foreground. I thought this is the SOLUTION to my problem!

Then I wrote the following script.

PROBLEM:

When I execute this script in the browser, the web page keeps LOADING, and the loading never finishes. Also, apparently, tshark keeps running in the background capturing packets, instead of stopping after 5 seconds. I AM SAYING THIS BECAUSE THE PACKETS KEEP ADDING IN THE CAPTURE FILES (capture.pcap and capture.txt - the files where packets captured are stored, see the command in the script) UNLESS I MANUALLY END THE LOADING OF THE WEBPAGE IN THE BROWSER.

Also, when I manually stop the loading of the web page and open the capture.pcap file in wireshark, I see that the packets which were supposed to be sent ONCE by the second command have been sent HUNDREDS of times.

QUESTIONS:

  1. Does this mean that both the commands are being run again and again and again, like in an infinite loop?

  2. There are no loops in the script. Then why is this infinite-loop like behavior? How can I fix this?

SCRIPT:

shell_exec("tshark  -a duration:5  -w /var/www/html/Test/capture.pcap -V > capture.txt &");

shell_exec("./mtu  -d0  -a43020008  -g43010008  -i987654321  -s'Mary'  2>&1");
I can't think of any reason why this would loop by itself. I'm not familiar with the mtu command, are you sure it only sends one packet?Barmar
Yeahhhh when I run the command directly from terminal, only one packet is sent. mtu is an application. Hey can you tell me a foolproof way of printing the output of the mtu command into a file or something. That will give me diagnostic information. I mean when I run the command directly in the terminal some lines of text are printed in the terminal which give diagnostic information. I need to see them now while running the command from php.Shy
mtu ... >shell_exec.out 2>&1 should capture all the output into the file.Barmar
Apparently only one packet is being sent.. at least the logs generated by mtu say so... but tshark captures 1000s of instances of that message.Shy
I don't know what's going on. Every day you post another example of this code that behaves in totally unexpected ways.Barmar