shellシミュレーションを同時実行します。

2936 ワード

  :
http://www.51testing.com/html/28/116228-238978.html
http://blog.chinaunix.net/uid-27571599-id-3473078.html 
  
     bash ,                。         ,       ,       。    ,      ,         “  ”     。
 
   :    (    :        IP           )
[root@ha-drbd-nfs-01-120 ~]# cat ip.txt 
192.168.0.24
192.168.0.25
192.168.0.26
192.168.0.27
192.168.0.28
192.168.0.29
192.168.0.40
192.168.0.41
192.168.0.42
192.168.0.43
192.168.0.81
[root@heartbeat-02-122 ~]# cat ping_host.sh
#!/bin/bash
 
array=$(cat ip.txt)
#echo $array
 
for host in ${array[@]}
do
        {
               ping $host -c 10 -w 1 -i 0.01 -q >/dev/null 2>&1
               ret=$?
               if [ $ret -eq 0 ];then
                        echo "$host isok"
               else
                        echo "$host isbad"
               fi
        }
done
[root@heartbeat-02-122 ~]# time sh ping_host.sh
192.168.0.24 is bad
192.168.0.25 is bad
……………………..
117.79.235.238 is ok
210.14.130.130 is ok
210.14.156.210 is ok
 
real    0m30.688s
user   0m0.099s
sys    0m0.501s
 
   :”   ”  (          )
[root@heartbeat-02-122 ~]# cat ping_host.sh
#!/bin/bash
 
array=$(cat ip.txt)
#echo $array
 
for host in ${array[@]}
do
        {
                ping $host -c 10 -w 1 -i0.01 -q >/dev/null 2>&1
                ret=$?
                if [ $ret -eq 0];then
                        echo"$host is ok"
                else
                        echo"$host is bad"
                fi
        }&
done
[root@heartbeat-02-122 ~]# time sh ping_host.sh
 
real   0m0.092s
user   0m0.006s
sys    0m0.014s
 
 :                &  ,               
  :           
 
   :     
         wait、read            ,            ,  ssh  、ping              cpu   ,          。
[root@heartbeat-02-122 ~]# cat ping_host.sh
#!/bin/bash
 
tmp_fifofile="/tmp/$.fifo"
mkfifo $tmp_fifofile                 
exec 6<>$tmp_fifofile                
rm $tmp_fifofile
 
thread=5                          
for ((i=0;i<$thread;i++));do
echo
done >&6                         
 
array=$(cat ip.txt)
#echo $array
 
for host in ${array[@]}
do                               
read -u6                           
{                                                   
    ping$host -c 10 -w 1 -i 0.01 -q >/dev/null 2>&1
    ret=$?
    if [$ret -eq 0 ];then
           echo "$host is ok"
    else
           echo "$host is bad"
    fi
       echo >&6                                    
} &
done
 
wait                                                 
exec 6>&-                                           
exit 0
[root@heartbeat-02-122 ~]# time sh ping_host.sh
192.168.0.27 is bad
192.168.0.26 is bad
192.168.0.25 is bad
…………………………….
210.14.156.210 is ok
210.14.130.130 is ok
117.79.235.238 is ok
 
real    0m6.247s
user   0m0.056s
sys    0m0.349s
この記事は、「鉡ちゃんの幸せな生活」ブログからの転載をお断りします。