代码的目标是,我想使用iperf进行随机tcp流量,并使用tcpdump在5,10,15,20秒内捕获它。此外,捕获吞吐量对我来说也很重要。我的问题是,我想在bash中执行5,10,15和20秒的code1,code2,code3和code4。但是我不知道如何为它提出上述条件。这是我的代码:
for Test_duration in 5 10 15 20
do
echo “Test performing with $Test_duration duration”
sudo tcpdump -G 10 -W 2 -w /tmp/scripttest_$Test_duration -i h1-eth0 &
while true; do
#code1
time2=$(($RANDOM%20+1))&
pksize2=$(($RANDOM%1000+200))&
iperf -c 10.0.0.2 -t $time2 -r -l $pksize2 >> /media/sf_sharedsaeed/throughtput/iperthroughput_host2_$Test_duration.txt &\
#code2
time3=$(($RANDOM%20+1))&
pksize3=$(($RANDOM%1000+200))&
iperf -c 10.0.0.3 -t $time3 -r -l $pksize3 >> /media/sf_sharedsaeed/throughtput/iperthroughput_host3_$Test_duration.txt &\
#code3
time4=$(($RANDOM%20+1))&
pksize4=$(($RANDOM%1000+200))&
iperf -c 10.0.0.4 -t $time4 -r -l $pksize4 >> /media/sf_sharedsaeed/throughtput/iperthroughput_host4_$Test_duration.txt &\
#code4
time5=$(($RANDOM%20+1))&
pksize5=$(($RANDOM%1000+200))&
iperf -c 10.0.0.5 -t $time5 -r -l $pksize5 >> /media/sf_sharedsaeed/throughtput/iperthroughput_host5_$Test_duration.txt &\
done
done
另一个限制是,code1,code2,code3和code4应该同时执行,因此,我使用&amp ;.
请帮助我应该替换while true;
代替定期执行代码。有谁能够帮我?
答案 0 :(得分:1)
您可以通过使用后台子shell来执行此操作,该子shell在您的while循环中检测到的到期时创建一个简单的文件锁。以下是基于代码简化版的示例:
<f:security.ifAuthenticated>
<f:then>
Logout
</f:then>
<f:else>
Login
</f:else>
</f:security.ifAuthenticated>
此代码的输出为:
for Test_duration in 5 10 15 20
do
# TIMEOUT_LOCK will be your file lock
rm -f TIMEOUT_LOCK
# next command will run in a parallel subshell at the background
(sleep $Test_duration; touch TIMEOUT_LOCK) &
echo “Test performing with $Test_duration duration”
while true; do
# check whether current timeout (5 or 10 or 15 ...) has occured
if [ -f TIMEOUT_LOCK ]; then rm -f TIMEOUT_LOCK; break; fi
# do your stuff here - I'm just outputing dots and sleeping
echo -n "."
sleep 1
done
echo ""
done