有时需要同时扫描几千个服务器,所有需要在shell中并发操作。虽然perl里可以很方便地对fork出来的进程做好
控制,但是有时就是一个小任务,所以直接就shell搞定了。
- #!/bin/bash
-
fun(){
-
echo "hello i am $1. $(date '+%T')"
-
sleep 1
-
}
-
i=0
-
PIDS=''
-
while read job
-
do
-
fun $job &
-
PIDS="$PIDS $!"
-
i=$((i+1))
-
if [ $i -ge 4 ];then
-
wait $PIDS
-
echo "finish jobs:$PIDS || $(date '+%T')"
-
PIDS=''
-
i=0
-
fi
-
-
-
done <list
- pm@debian:~$ time ./multi.sh
-
hello i am 1. 23:41:23
-
hello i am 4. 23:41:23
-
hello i am 2. 23:41:23
-
hello i am 3. 23:41:23
-
finish jobs: 13482 13483 13485 13487 || 23:41:24
-
hello i am 5. 23:41:24
-
hello i am 8. 23:41:24
-
hello i am 6. 23:41:24
-
hello i am 7. 23:41:24
-
finish jobs: 13495 13496 13497 13500 || 23:41:25
-
hello i am 9. 23:41:25
-
hello i am 11. 23:41:25
-
hello i am 12. 23:41:25
-
hello i am 10. 23:41:25
-
finish jobs: 13508 13509 13510 13513 || 23:41:26
-
hello i am 13. 23:41:26
-
hello i am 14. 23:41:26
-
hello i am 15. 23:41:26
-
hello i am 16. 23:41:26
-
finish jobs: 13521 13522 13524 13526 || 23:41:27
-
-
real 0m4.049s
-
user 0m0.012s
-
sys 0m0.000s
如果把后台任务数修改为8的话
- pm@debian:~$ time ./multi.sh
-
hello i am 7. 23:43:54
-
hello i am 5. 23:43:54
-
hello i am 3. 23:43:54
-
hello i am 8. 23:43:54
-
hello i am 4. 23:43:54
-
hello i am 1. 23:43:54
-
hello i am 6. 23:43:54
-
hello i am 2. 23:43:54
-
finish jobs: 13597 13598 13600 13602 13603 13604 13606 13607 || 23:43:55
-
hello i am 9. 23:43:55
-
hello i am 10. 23:43:55
-
hello i am 11. 23:43:55
-
hello i am 15. 23:43:55
-
hello i am 14. 23:43:55
-
hello i am 12. 23:43:55
-
hello i am 13. 23:43:55
-
hello i am 16. 23:43:55
-
finish jobs: 13622 13623 13625 13627 13628 13630 13631 13632 || 23:43:56
-
-
real 0m2.061s
-
user 0m0.004s
-
sys 0m0.008s
如果觉得没有必要记录后台的PID
也可以直接使用这样的形式:
- fun(){
-
echo "hello i am $1. $(date '+%T')"
-
sleep 1
-
}
-
i=0
-
while read job
-
do
-
fun $job &
-
i=$((i+1))
-
if [ $i -ge 8 ];then
-
wait
-
echo "finish jobs|| $(date '+%T')"
-
i=0
-
fi
-
-
-
done
阅读(2452) | 评论(0) | 转发(0) |