Share a concurrent script that simulates multithreading under Linux, using this script to execute related commands on a defined number of servers at the same time, which is much more efficient than a normal for/while loop and can be executed in a single execution, and is very useful when managing a large number of servers.
The following scripting features update packages through SCP (or rsync) up to thousands of servers, and after the script runs, there are 50 SCP processes in the background that pass packets to the server.
#!/bin/bash
Ip= ' Cat iplist.txt|grep-v ' # ' |awk ' {print $ ' #过滤服务器IP
Dir= '/usr/local/src ' #目标路径
Thead_num=50 #自定义并发数, according to their own server performance or application sizing, start do not define too large, to avoid the management of the machine downtime
Tmp_fifo_file= "/tmp/$$.fifo" #以进程ID号命名管道文件
Mkfifo $tmp _fifo_file #创建临时管道文件
EXEC 4<> $tmp _fifo_file #以读写方式打开tmp_fifo_file管道文件, file descriptor 4, or 3-9 arbitrary descriptor
Rm-f $tmp _fifo_file #删除临时管道文件, or do not delete
For ((i=0;i< $thead _num;i++)) #利用for循环向管道中输入并发数量的空行
Do
echo "" #输出空行
Done >&4 #输出重导向到定义的文件描述符4上
For I in $ip #循环所有要执行的服务器
Do
Read-u4 #从管道中读取行, one line at a time, after all rows have been read, execution hangs until the pipeline has idle rows
{
Scp-p $i: $dir #所有要批量执行的命令都放在大括号内, SCP is a simple instance, can replace any other command and command group, 1000 is server port
Sleep 3 #暂停3秒, which gives the system buffer time to limit the number of concurrent processes
echo "" >&4 #再写入一个空行 so that the pending loop continues execution
}& #放入后台执行
Done
Wait #等待所有后台进程执行完成
EXEC 4>&-#删除文件描述符
Exit 0
--------------------------------low-key split-line------------------------------------
If the management machine does not have an SSH trust with other servers, you can also add the Expect auto answer command to the loop body of the concurrent script, as follows:
#!/bin/bash
Ip= ' Cat iplist.txt|grep-v ' # ' |awk ' {print '} '
Dir= '/USR/LOCAL/SRC '
Answer= "yes" #定义yes/no response variable
Passwd= "123456" #服务器密码
Thead_num=50
Tmp_fifo_file= "/tmp/$$.fifo"
Mkfifo $tmp _fifo_file
EXEC 4<> $tmp _fifo_file
Rm-f $tmp _fifo_file
For ((i=0;i< $thead _num;i++))
Do
echo ""
Done >&4
For I in $ip
Do
Read-u4
{
Expect <<eof
Set Timeout-1
Spawn scp-p $i: $dir
Expect "(yes/no)?" {
Send "$answer \ r"
Expect "Password:"
Send "$passwd \ r"
} "Password:" {send "$passwd \ r"} "*host" {Exit 1}
Expect EOF
Eof
Sleep 3
echo "" >&4
}&
Done
Wait
EXEC 4>&-
Exit 0
Concurrent concurrent shell scripts simulating multithreading in Linux