linux shell 实现多进程

时间:2014-08-08 16:22:36   收藏:0   阅读:250

作为linux系统运维或者linux下的数据库DBA,很多时候需要写一些脚本来帮组我们实现某些需求,如果脚本内的某些内容能够试下并行处理,将大大提高工作的速度。

不多说,上脚本

先举一个顺序执行的例子:

[root@xx test]# cat test.sh

#!/bin/bash

for i in {1..5};do

sleep 1 ; echo "hello"

done

[root@xx test]# time sh test.sh

hello

hello

hello

hello

hello


real 0m5.016s

user 0m0.001s

sys 0m0.001s


这里的耗时是5.016s

---------------------------------------------------------

简单的改进一下

---------------------------------------------------------

[root@xx test]# cat test.sh 

#!/bin/bash

for i in {1..5};do

{

sleep 1 ; echo "hello"

}&

done

wait    #等待上面的程序结束

[root@xx test]# time sh test.sh 

hello

hello

hello

hello

hello


real 0m1.008s

user 0m0.004s

sys 0m0.005s

这里耗时为1.008秒

本文出自 “linux运维之路” 博客,转载请与作者联系!

linux shell 实现多进程,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!