1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 并发执行linux命令结果混乱 Shell脚本--并发执行

并发执行linux命令结果混乱 Shell脚本--并发执行

时间:2018-08-28 13:41:03

相关推荐

并发执行linux命令结果混乱 Shell脚本--并发执行

#Shell脚本中并发执行

顺序执行

#!/bin/bash

echo"Testorderexecution"

start=$(date+%s)

for((i=0;i<5;i++))

do

sleep2

echo"Done$i"

done

end=$(date+%s)

take=$((end-start))

echo"Orderexecutiontaketime(s):$take"

$bashtest_order_exec.sh

Testorderexecution

Done0

Done1

Done2

Done3

Done4

Orderexecutiontaketime(s):10

2. 并发执行

#!/bin/bash

echo"Testconcurrentexecution"

start=$(date+%s)

for((i=0;i<5;i++))

do

{

sleep2

echo"Done$i"

}&

done

wait

end=$(date+%s)

take=$((end-start))

echo"Concurrentexecutiontaketime(s):$take"

$bashtest_concurrent_exec.sh

Testconcurrentexecution

Done2

Done0

Done3

Done4

Done1

Concurrentexecutiontaketime(s):2

实际运用:

如果想模拟并发对数据库的更新操作,如更新某一个统计值,如访问次数。可以通过如下的脚本来实现:

#!/bin/bash

for((i=0;i<100;i++))

do

mysql-uroot-p123456-Dtest-e"updatecount_tsetcount=count+1;"&

done

补充:

去除恼人的Warning提示

$mysql-uroot-proot-e"selectnow()"

Warning:Usingapasswordonthecommandlineinterfacecanbeinsecure.

+---------------------+

|now()|

+---------------------+

|-07-0520:17:05|

+---------------------+

#清空Warning提示2代表标准错误输出

$mysql-uroot-proot-e"selectnow()"2>/dev/null

+---------------------+

|now()|

+---------------------+

|-07-0520:17:53|

+---------------------+

#清空所有输出2等同于1(标准输出)

$mysql-uroot-proot-e"selectnow()">/dev/null2>&1

参考文档:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。