1

I'm debugging a shell script so I add set -x at the beginning a code snippet are as below

tcpdump -i $interface host $ip_addr and 'port 80' -w flash/flash.pcap &
sudo -u esolve firefox /tor_capture/flash.html &
sleep $capture_time

but I noticed that the execution sequence is as below

++ sleep 5
++ sudo -u esolve firefox /tor_capture/flash.html
++ tcpdump -i eth0 host 138.96.192.56 and 'port 80' -w flash/flash.pcap
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

so the execution sequence is reversed compared to the command sequence in the script what is wrong with this and how to deal with it? thanks!

2 Answers 2

1

Since those lines are being backgrounded, I think the output from set -x comes from the subshell that is spawned to run the program, and the main shell gets to the sleep command before the subshells have proceeded to the point that they generate the output. That would explain why the sleep command shows up first. With regards to the other two, I would think you might occasionally get them in the other order, as well, since there's no synchronization between the two - depending on how many CPUs you have, how busy the system is, etc., the timing between the subshells is pseudo-non-deterministic...

Sign up to request clarification or add additional context in comments.

2 Comments

so in a shell script how can I make these three command run in the order as i specified?
They are being started in the order that you specify. They just don't get to the point where they log that fact in that same order. That's inherent in running things in the background/concurrently... If you need to guarantee that, for example, tcpdump is completely initialized before you start firefox, put some sleep 5 lines in between or something...
0

Do you need the first 2 lines to run as background processes?

If not, remove the & at the end and try again.

1 Comment

I need them to run as background processes, coz I need to capture packets for some time and then kill these tcpdump and firefox

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.