#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
参考文档: