shell的单、多、控制进程的使用方法 一、100个任务使用200秒(普通完成) cat process2.sh #!/bin/bash function aaa () { start=`date "+%s"` for ((i=1;i<=100;i++)) do echo test;sleep 2 done end=`date "+%s"` echo "time:`expr $end - $start`" } aaa exit 0 ================执行结果================= time:200 real 3m20.338s user 0m0.032s sys 0m0.008s 二、100个任务使用2秒(非控制进程完成任务) cat process1.sh #!/bin/bash function aaa () { start=`date "+%s"` for ((i=1;i<=100;i++)) do ( echo test;sleep 2 )& done wait end=`date "+%s"` echo "time:`expr $end - $start`" } aaa exit 0 ================执行结果================= time bash process1.sh time:2 real 0m2.259s user 0m0.008s sys 0m0.020s 三、100个任务使用20秒(控制进程完成任务,生产环境使用) cat process.sh function aaa () { start=`date "+%s"` for ((i=1;i<=100;i++)) do ( echo 111;sleep 2 )& [[ $i%10 -eq 0 ]] && wait done wait end=`date "+%s"` echo "time:`expr $end - $start`" } aaa exit 0 ================执行结果================= time bash process.sh time:20 real 0m20.376s user 0m0.000s sys 0m0.032s
时间: 2024-12-15 09:53:33