shell脚本使用 timeout + wait 完成: 超时退出执行,等待执行完毕并处理执行结果 - yanbin's Blog

shell脚本使用 timeout + wait 完成: 超时退出执行,等待执行完毕并处理执行结果

yanbin posted @ 2015年6月28日 17:52 in Programming with tags linux shell programming , 20996 阅读
具体需求是:
1.从文件中读取 seq, 使用 pub 程序将 seq 推送给定阅读了 cmd topic 的 peer client.
   client 将处理结果(message)推送到 cmdresp topic. 这个过程是经过 server 的,可以不考虑。
   pub 成功后记录 seq.
2. sub 程序订阅 cmdresp topic, 简单处理接收到的消息,并且记录到文件。
3. 对比 pub 成功的 seq 记录与接收到的 message, 获得一个 pub/sub 成功率。
 
问题是:
1. pub 和 sub 分别使用两个不同的管道,pub cmd 成功后并不等待 cmdresp.
   甚至可以说: pub 程序并不知道 cmdresp 的存在,甚至不知道 sub 的存在.
2. sub 是阻塞的:没有任何消息也不退出。
3. pub message 到 peer client 到 cmdresp 返回之间的时间是不确定的。
   消息在两个管道都有可能出现延迟。
4. sub 程序本身没有超时退出选项。
 
伪代码是:
timeout time sub cmdresp_topic > cmdresp_record
while seq in read seqs:
     seq = parse_seq(seq)
     message = create_message(seq)
     pub cmd_topic message
     record(message, msg_record)
     // do something, maybe sleep 1s

wait sub

parse(cmdresp_record, msg_record)

shell 代码是:
 

tmp_file="$(mktemp)
rm -f $tmp_file
mkfifo $tmp_file

# terminate $MOSQUITTO while $TIMEOUT period
timeout $TIMEOUT \
$MOSQUITTO_SUB -t $topic_resp --cafile $CA --cert $CERT --key $KEY > $tmp_file &
cat $tmp_file | cut -d',' -f1 | cut -d':' -f2 | sed -n 's/\"//gp' >> $SUB_LOG &

N=0
for line in $(cat $SNS_FILE)
do
sn="$(echo "$line" | cut -d'&' -f1)"
seq="$($LOOKUP_TOPIC $sn)"
name="$(printf "%s%.4d" $sn $seq)"
topic="router/$name/cmd"

N=$((N+1))
mid="$(date +%Y%m%d%H%M%S)-$N"
message=makemsg $mid

$MOSQUITTO_PUB -t $topic --cafile $CA --cert $CERT --key $KEY -m "$message"
sw="$(echo "$line" | cut -d'&' -f2)"
record="$(date +%Y%m%d:%H),$sn,$sw,1"
[ $? -eq 0 ] && echo "$record" >> $PUB_LOG
[ $((N % 10)) -eq 0 ] && sleep $SLEEP_TIME
done

wait # wait $MOSQUITTO_SUB and cat $tmpfile terminaation
rm -f $tmp_file
if [ -f $SUB_LOG -a -f $PUB_LOG ]; then
tmp_file="$(mktemp -p $RECORD_DIR)"
for line in $(cat $SUB_LOG)
do
sed "s/\(.*$line.*\),1/\1,0/g" $PUB_LOG > $tmp_file
cp $tmp_file $PUB_LOG
done
rm -f $tmp_file
fi

 

Avatar_small
impossible game 说:
2018年12月26日 10:43

I have a similar issue. I will try your solution and see if it works

Avatar_small
Allassignmenthelp.co 说:
2019年1月29日 18:39

Hello guys do you know that going through a review of websites will give you much insight about it which you can’t know by simply visiting it, Here is the Allassignmenthelp.co.uk Reviews that reviews the website and provides you the best website .

Avatar_small
먹튀다자바 说:
2019年8月01日 16:03

I think more writers should take care to write with passion like you. Even informational articles like this can have personality. That’s what you have interjected in this informative article. Your views are very unique.

Avatar_small
ucapan selamat 说:
2019年9月07日 01:08

I have a similar issue. I will try your solution and see if it works

Avatar_small
ucapan selamat 说:
2019年9月07日 01:08

I have a similar issue. I will try your solution and see if it works

Avatar_small
فوائد الزنجبيل 说:
2019年9月14日 16:01

Once i received on your own website despite the fact that inserting attention simply a bit small bit submits. Pleasurable technique for lasting, I will be book-marking at any moment receive sorts full will come way up.

Avatar_small
VIP Financing Soluti 说:
2019年9月23日 06:09

Very nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed surfing around your blog posts. In any case I will be subscribing to your feed and I hope you write again soon!

Avatar_small
regeneracja turbin B 说:
2019年10月04日 01:11

When do you think this Real Estate market will go back up? Or is it still too early to tell? We are seeing a lot of housing foreclosures in Casselberry Florida. What about you? We would love to get your feedback on this.

Avatar_small
rental car Los Angel 说:
2019年10月21日 04:30

some cheap softwares does not offer good online technical support so i would caution about using them`

Avatar_small
run 3 说:
2019年11月22日 12:42

Thanks for sharing. I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one.

Avatar_small
mariachi bogota 说:
2019年11月23日 05:30

An fascinating discussion might be priced at comment. I’m sure that you simply write much more about this topic, it might not be a taboo subject but generally individuals are inadequate to dicuss on such topics. To another. Cheers

Avatar_small
paginas web bogota 说:
2019年11月23日 05:31

Pleasant blog post. It appears that plenty of the stages are counting on the inventiveness element. “Bad mind, bad heart.” by Anacharsis Cloots..

Avatar_small
optica alemana 说:
2019年11月23日 05:32

I am often to blogging we actually appreciate your posts. The content has truly peaks my interest. I am going to bookmark your web site and maintain checking achievable data.

Avatar_small
webmaster@correo.com 说:
2019年11月27日 22:20

Nice blog

Avatar_small
webmaster@correo.com 说:
2019年11月27日 22:21

Nice blog

Avatar_small
www.pixelpro.com.co 说:
2019年11月27日 22:22

Nice blog

Avatar_small
mariachi bogota 说:
2019年11月30日 03:24

When I originally commented I clicked the -Notify me when new surveys are added- checkbox and already each time a comment is added I get four emails using the same comment. Perhaps there is in any manner you are able to eliminate me from that service? Thanks!

Avatar_small
paginas web 说:
2019年11月30日 03:26

I am perpetually thought about this, thankyou for putting up.

Avatar_small
fireboy and watergir 说:
2020年3月24日 18:07

This is a great little post with some valuable tips. I totally agree. The way you bring passion and engagement into the things you do can really change your outlook on life.

Avatar_small
Bassnectar Merch 说:
2020年3月30日 18:07

I think this is a better than average article. You make this data intriguing and locks in. You give perusers a considerable measure to consider and I welcome that sort of composing

Avatar_small
reverent-meitners.ne 说:
2020年4月09日 23:02

This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses.

Avatar_small
dental masks 说:
2020年4月28日 20:12

I appreciated your work very thanks

Avatar_small
Realizzazione Siti W 说:
2020年4月29日 21:29

We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work.

Avatar_small
New rap songs 2020 说:
2020年5月03日 23:24

I appreciate everything you have added to my knowledge base.Admiring the time and effort you put into your blog and detailed information you offer.Thanks.

Avatar_small
Information Tech 说:
2020年11月02日 12:37

That's the reason concentration on make sure you targeted foot work some time before writing. Would be practical to make sure you a great deal more sensible short article in this way.

Avatar_small
sad whatsapp status 说:
2021年6月17日 21:03

Sad images was available here on this website this is an awesome post for the user.

Avatar_small
Bangalore Escorts 说:
2023年2月08日 20:38

This article was really very good, I liked Pernal very much, I wish that you also read this article once, you will also enjoy Jake on this side.

Avatar_small
poojapandit 说:
2023年2月08日 21:13

Versova Escorts Service || Malabar Hill Escorts || Escorts in Bandra || Panvel Escort || Lucknow Escorts Service || Hyderabad Escorts || Escorts in Kochi || Thane Escort || Jaipur Escorts || Escorts in Worli || A hot young girl is sitting tight for you, only a couple of moments away from you. One call can change your life's sexual experience. Our sexy hot model's SMALL AND TIGHT TITS

Avatar_small
poojapandit 说:
2023年2月08日 21:15

ready to have incredible and energizing occasions outside your city after your riotous business plan, isn't that so? On the off chance that you are one, you get the opportunity to have the most satisfying and qualified escort young girls to discuss and have the flavours. In the quest for joy and importance

Abhanpur Escort || Arang Escorts || Escorts in Baloda Bazar || Bindranavagarh Escorts Service || Chhura Escort || Deobhog Escorts || Escorts in Kasdol || Mainpur Escorts Service || Kishangarhh Escort || Rajim Escorts || Escorts in Tilda || Bilaigarh Escorts Service ||

Avatar_small
Akriti 说:
2023年4月08日 14:14

We also have an option of offering the Escorts in Bangalore for our clients. All you have to do is tell us, and we will send them for you immediately.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee