linux shell 實現多進程

作爲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秒

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章