SoFunction
Updated on 2025-03-10

Example of method for shell scripts to implement multi-process running

This article mainly shares with you the relevant content of shell scripts to implement multi-process operation. It is shared for your reference and learning. Let’s take a look at the detailed introduction together:

example

for ip in 192.168.56.{1..254}
do
 (
 ping $ip -c 4 &>/dev/null;
 if [ $? -eq 0 ]; then
  echo $ip is alive
 fi
 ) &
done
wait

explain

The key point is () and &, and the commands that need to be executed in for are used as a combination and run in the background.

wait for all background subroutines to be executed

Execution efficiency

Use time to view the running time. I don't need to say much about this efficiency.

time bash 
192.168.56.101 is alive
192.168.56.1 is alive
192.168.56.102 is alive
192.168.56.103 is alive
real 0m13.164s
user 0m0.157s
sys 0m1.160s

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.