Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell multithreading operation and thread number control method tutorial

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "Shell multithreading operation and thread number control method tutorial", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "Shell multithreading operation and thread number control method tutorial"!

Demand

In order to better illustrate the problem, let's explain it with an example, assuming that the requirement is to scan the url.txt file and then determine whether the URL in it is invalid. The content of the url.txt file is one URL per line, such as:

The code is as follows:

Http://www.baidu.com

Http://www.google.com

Https://www.yisu.com

Single process implementation

Then the shell script scanUrl.sh can be written as follows:

The code is as follows:

#! / bin/bash

# determine whether there are parameters

If [$#! = 1]; then

Echo "The parameters you enter is not correct!"

Exit-1

Fi

# read the URL and judge the status code in a loop

While read line

Do

{

Isok= `curl-I-o / dev/null-s-w% {http_code} $line`

If ["$isok" = "200"]; then

Echo $line "OK"

Else

Echo $line "no"

Fi

}

Done

< $1 echo "执行结束" 那么可以执行下面的命令扫描: 代码如下: /bin/sh scanUrl.sh url.txt 但这样脚本执行非常慢,一万个URL几个小时都扫描不完。 多进程实现 改成多进程实现非常简单,只需要在do后面的大括号加 & 符号,在done后面加一个wait,表示父进程等待子进程退出后再退出 代码如下: #!/bin/bash #判断是否有参数 if [ $# != 1 ] ;then echo "The parameters you enter is not correct !"; exit -1; fi #循环读出URL并判断状态码 while read line do { isok=`curl -I -o /dev/null -s -w %{http_code} $line` if [ "$isok" = "200" ]; then echo $line "OK" else echo $line "no" fi } }& done < $1 wait echo "执行结束" 这样就能多进程并发执行了,但有个问题是进程会一下子非常多,几百上千,超过系统限制报错,下面我们就加上进程数控制。 多进程实现并控制进程数 代码如下: #!/bin/bash #允许的进程数 THREAD_NUM=200 #定义描述符为9的管道 mkfifo tmp exec 9tmp #预先写入指定数量的换行符,一个换行符代表一个进程 for ((i=0;i&9 done if [ $# != 1 ] ;then echo "The parameters you enter is not correct !"; exit -1; fi while read line do { #进程控制 read -u 9 { #isok=`curl -I -o /dev/null -s -w %{http_code} $line` if [ "$isok" = "200" ]; then echo $line "OK" else echo $line "no" fi echo -ne "\n" 1>

& 9

} &

}

Done

< $1 wait echo "执行结束" rm tmp 上面的代码就可以保证子进程在指定数量了,其进程控制原理是通过管道实现的,当管道无内容可读时就不会执行 代码如下: { #isok=`curl -I -o /dev/null -s -w %{http_code} $line` if [ "$isok" = "200" ]; then echo $line "OK" else echo $line "no" fi #写入一个换行符 echo -ne "\n" 1>

& 9

} &

And after each process completes execution, a newline character is written to the pipe to ensure that the number of processes is specified.

So that we can achieve our goal.

At this point, I believe that everyone on the "Shell multithreaded operation and thread number control method tutorial" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report