Simulate concurrent shell scripts with multiple threads in Linux

Source: Internet
Author: User
Share a concurrent script that simulates multiple threads in Linux. You can use this script to execute related commands simultaneously on a defined number of servers, compared with a common for/while LOOP, a single execution can only be executed one by one in sequence, which is very practical in managing a large number of servers. The following Script Function uploads an update package to thousands of servers through scp (or rsync, after the script is run, 50 scp processes in the background share a concurrent script simulating multiple threads in Linux to the server. With this script, you can execute commands on the defined number of servers simultaneously, compared with a common for/while LOOP, a single execution can only be executed one by one in sequence, which is very practical in managing a large number of servers.
The following script function transfers an update package to thousands of servers through scp (or rsync). After the script is run, 50 scp processes in the background will transfer the package to the server.
#! /Bin/bash
Ip = 'cat iplist.txt | grep-v "#" | awk '{print $1} ''# filter Server IP Address
Dir = '/usr/local/src' # target path

Thead_num = 50 # customize the number of concurrent jobs. Adjust the size based on your server performance or application. do not define too large at first to avoid management downtime.
Tmp_domaino_file = "/tmp/$. fifo" # name the MPs queue file with the process ID.
Mkfifo $ tmp_kerbero_file # create a temporary MPs queue File
Exec 4 <> $ tmp_fifo_file # Open the tmp_fifo_file pipeline file in read/write mode. The file descriptor is 4. You can also use any descriptor 3-9.
Rm-f $ tmp_kerbero_file # Delete temporary MPs queue files.
For (I = 0; I <$ thead_num; I ++) # Use the for loop to input the number of concurrent empty rows to the pipeline.
Do
Echo "" # Empty output line
Done> & 4 # output redirection to defined file descriptor 4


For I in $ ip # loop through all servers to be executed
Do
Read-u4 # read a row from the MPs queue. After reading all the rows, the row is suspended until the MPs queue has idle rows.
{
Scp-P 1000 $1 $ I: $ dir # All commands to be executed in batches are placed in braces. scp is a simple instance that can replace any other commands and command groups, 1000 is the server port
Sleep 3 # Pause for 3 seconds, buffer the system time, and limit the number of concurrent processes
Echo ""> & 4 # Write a blank row to continue the suspended loop.
} & # Put It In the background for execution
Done
Wait # wait until all background processes are completed
Exec 4> &-# delete a file descriptor
Exit 0

------------------------------ Low-key split line ------------------------------------
If the Administrator and other servers do not have an ssh trust, you can also add the automatic response command of the secondary CT to the loop body of the concurrent script. Modify the following:
#! /Bin/bash
Ip = 'cat iplist.txt | grep-v "#" | awk '{print $1 }''
Dir = '/usr/local/src'
Answer = "yes" # define the yes/no response variable
Passwd = "123456" # server password
Thead_num = 50
Tmp_domaino_file = "/tmp/$. fifo"
Mkfifo $ tmp_kerbero_file
Exec 4 <> $ tmp_kerbero_file
Rm-f $ tmp_kerbero_file
For (I = 0; I <$ thead_num; I ++ ))
Do
Echo ""
Done> & 4
For I in $ ip
Do
Read-u4
{
Secondary CT < Set timeout-1
Spawn scp-P 1000 $1 $ I: $ dir
CT "(yes/no )? "{
Send "$ answer \ r"
Reset CT "Password :"
Send "$ passwd \ r"
} "Password:" {send "$ passwd \ r"} "* host" {exit 1}
CT eof
EOF

Sleep 3
Echo ""> & 4
}&
Done
Wait
Exec 4> &-
Exit 0

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.