Share an entry-level controllable multithreaded shell script code _linux Shell

Source: Internet
Author: User

When it comes to shell-controlled multithreading, most of the online sharing is a pipeline control scheme. This scheme, Zhanggo blog has also been actual combat and share once: "Shell+curl website health status check script, grabbed the Chinese blog Alliance lost the site", interested friends can see.

Share an entry-level controllable multithreaded shell script scenario

Below Zhanggo Blog to share another easier to understand the entry-level controllable multithreaded shell Scripting scenario: task cutting, splitting.

Here's a 1-paragraph scenario Description:

One day, in the Goose factory received this task, need to in the Linux server, thousands of IP for a ping test, as long as the ping can reach the IP is good. If a single IP to ping test, although also can complete the task, thousands of IP OK, if more?

Given the simplicity of the case, the first time to give up the previously used piping plan, but the use of the idea of a broken.

Simple idea:

According to the task of cutting "strategic thinking", I first put the thousands of IP into a iplist file, and then write a split function, the file into a number of temporary IP list, and finally, using multithreading to traverse these temporary IP files can be implemented in a disguised form of multithreading.

Specific code:

 #!/bin/sh #文本分割函数: Split the text by $ $ splitfile () {linenum= ' wc-l ' "|awk ' {print} ' if [[$linenum-le $]] then echo "The lines of this file is less then $ Are you kidding me ..." Exit fi split= ' expr $linenum/$ ' num1=1 filenum=1 test-d splitfile | | Mkdir-p splitfile rm-rf splitfile/* while [$Num 1-lt $linenum] do num2= ' expr $Num 1 + $Split ' sed-n ' ${num1}, ${n 
Um2}p "$ > splitfile/$1-$FileNum num1= ' expr $Num 2 + 1 ' filenum= ' expr $FileNum + 1 ' done} #Define some variables SPLIT_NUM=${1:-10} #参数1表示分割成多少份即, how many threads to open, default 10 file=${2:-iplist} #参数2表示分割的对象, default IPList file #分割文件 splitfile $FILE $SPLIT _ NUM #循环遍历临时IP文件 for IPList in $ (ls./splitfile/*) do #循环ping测试临时IP文件中的ip (lost background) Cat $iplist | While read IP do ping-c 4-w 4 $ip >/dev/null && echo $ip | Tee-ai okip.log #ping IP is written to the log done & #在while循环后面加上 & symbol, let this nested loop execute in the background 

After you save the code as ping.sh, the process of performing SH ping.sh iplist 100 is as follows:
First cut the iplist into 100 parts and store them in the Splitfile folder.

The split file is then read through a for loop, and the IP is ping in the background using a while loop.
Because while is lost backstage, the for loop executes 100 while, which is equivalent to opening 100 threads, which is a natural speed.
Among them, cut the number of copies that you want to open the number of multithreading, it is obvious that this task segmentation of the idea, although no pipeline scheme to the tall, but its ideas more simple and understandable, but also better versatility, suitable for entry-level simple multithreaded tasks.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.