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

How to execute Linux commands concurrently

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains "how to execute Linux commands concurrently". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to execute Linux commands concurrently.

Serial is the execution of one command after another in order, as opposed to serial, where multiple commands are executed at the same time, which is called parallel.

Use

In bash scripts, there are many uses for concurrent execution of commands, such as batch uploading and downloading, batch starting and closing programs, bulk packaging logs, batch checking whether remote machines are reachable, and so on.

Parallel execution can make full use of system resources, greatly improve efficiency and save a lot of time.

For example, 20 files need to be downloaded, assuming that each file takes 1 minute to download

Serial download is when one file is downloaded and then another file is downloaded, so it takes 20 minutes to download 20 files.

Parallel download is to start 20 processes, 20 processes download at the same time, each process is responsible for downloading a file, so that in theory, all 20 files can be downloaded in about 1 minute.

Serial and parallel

Adding the & symbol after the command indicates that the command will be executed in the child process. Here are two examples that compare serial and parallel.

C.sh script

#! / bin/bash func_a () {echo "func_a..$1.." Sleep 1} for n in $(seq 1 5); do func_a $n done echo "c.sh.. finish."

Execute time. / c.sh, and the result is as follows

[root@ecs-centos-7 mult] # time. / c.sh func_a..1.. Func_a..2.. Func_a..3.. Func_a..4.. Func_a..5.. C.sh..finish.. Real 0m5.008s user 0m0.005s sys 0m0.003s

In the above example, the func_a function is called five times in a row, and each call will sleep for 1 second, and the next call can not be made until the last call is over, so the five calls took a total of 5 seconds.

Modify the c.sh script as follows

#! / bin/bash func_a () {echo "func_a..$1.." Sleep 1} for n in $(seq 1 5); do func_a $n & done wait echo "c.sh.. finish."

Execute time. / c.sh again and the result is as follows

[root@ecs-centos-7 mult] # time. / c.sh func_a..1.. Func_a..3.. Func_a..2.. Func_a..4.. Func_a..5.. C.sh..finish.. Real 0m1.006s user 0m0.004s sys 0m0.005s

The modified script adds an & symbol after the func_a $n statement in the loop to indicate that the func_a $n command is executed in the child process.

At the end of the loop, there is a wait statement indicating that the following command will not be executed until all the subprocesses in the body of the previous loop are finished.

Since the child process does not block the current process, the current process can proceed to the next call, so a total of 5 calls took only about 1 second

During the time. / c.sh command, open another terminal and enter ps aux | enter grep c.sh, and the output is as follows

[root@ecs-centos-7 ~] # ps aux | grep c.sh root 29086 0.0 113188 1400 pts/1 S + 22:55 0:00 / bin/bash. / c.sh root 29088 0.0 113188 628 pts/1 S + 22:55 0:00 / bin/bash. / c.sh root 29089 0.0 113188 628 pts/1 S + 22:55 0:00 / bin/bash. / c .sh root 29090 0.0 113188 628 pts/1 S + 22:55 0:00 / bin/bash. / c.sh root 29091 0.0 113188 628 pts/1 S + 22:55 0:00 / bin/bash. / c.sh root 29093 0.0 113188 628 pts/1 S + 22:55 0:00 / bin/bash. / c.sh root 29099 0.0 112728 972 Pts/2 S + 22:55 0:00 grep-color=auto c.sh

As you can see from the output above, there are six c.sh script processes at the same time, of which five are child processes generated by five loops, and the remaining one is a process that executes the c.sh script itself.

Check whether the host is reachable

When there are many remote hosts that need to be managed, some checks are often made on these hosts, and batch checking whether the host is reachable is a common operation. Here is a brief description of how to use concurrent execution to quickly check whether the host is reachable.

If there is a host.txt file, the IP address of the host is stored in it. The details are as follows

[root@ecs-centos-7 mult] # cat host.txt 192.168.0.1 192.168.0.2 192.168.0.3 192.168.0.4 192.168.0.5 192.168.0.6 192.168.0.7 192.168.0.8 192.168.0.9

C.sh script, which contains the following

#! / bin/bash ping_test () {ping $1-c 2-W 3 & > / dev/null if [$?-eq 0]; then echo "$1 reachable..." Else echo "$1 unreachable..." Fi} for n in $(cat host.txt); do ping_test $n & done wait echo "c.sh.. finish."

Execute time. / c.sh, and the result is as follows

[root@ecs-centos-7 mult] # time. / c.sh 192.168.0.1 reachable... 192.168.0.9 reachable... 192.168.0.4 unreachable... 192.168.0.3 unreachable... 192.168.0.6 unreachable... 192.168.0.2 unreachable... 192.168.0.8 unreachable... 192.168.0.7 unreachable... 192.168.0.5 unreachable... C.sh..finish.. Real 0m3.010s user 0m0.014s sys 0m0.011s

The ping $1-c 2-w 3 statement in the script is the command of the ping remote host,-c 2 indicates the number of packets sent, and-W 3 sets the timeout to 3 seconds

From the test results, it can be seen that 192.168.0.1 and 192.168.0.9 hosts are reachable, while other hosts are unreachable, and it took a total of about 3 seconds.

Thank you for your reading, the above is the content of "how to execute Linux commands concurrently". After the study of this article, I believe you have a deeper understanding of how to execute Linux commands concurrently, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Servers

Wechat

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

12
Report