Using screen to implement near-concurrent shell Remote Call and partial implementation

Source: Internet
Author: User

Assume that the same Command needs to be executed on multiple nodes (considering the uncertainty of the command). Generally, the for loop is used and the command can be directly called through ssh. For example, you need to view the root directory usage of each node:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1HT535K-0.jpg "title =" ssh-for.jpg "/>



In fact, you only need to write the preceding commands into a script and set the commands to be executed as location parameters to meet these requirements in a trusted cluster. It seems that the problem has been solved, I did this before, but the problem arises, and some commands have a long execution process. However, when executing some commands in order, the waiting time is definitely a crash. For example, if you update the database (yum update) for each node or generate the yum cache (yum makecache) for hundreds of node clusters ), or each node is new, and many disks need to be formatted and mounted. If the for loop is executed in order, believe me, the waiting time of the formatted disk will be unbearable, if this task must be completed at the specified time .......


Okay, for a long time, the demand has emerged. Let's first think about the problem to be solved, and then implement and test it in a simple part.

1. All nodes are mutually trusted, which can be achieved during system installation)

2. All nodes can use regular expressions to retrieve the corresponding IP address list script)

3. You must have installed the screen package and call this command to implement almost concurrent task execution scripts)

4. Obtain the return value of the command execution result to determine whether the execution is successful or not)

5. The screen resource release issue is not implemented)

6. Failed to handle network faults)

7. Failed error handling considerations)

8. If you need to execute too many tasks on each node, you can write the tasks to be executed into a simple execution script, push them, and control the execution; this method can be considered for implementation of 4, 5, 6, and 7.

9. Consideration of environment variables during Remote Command Execution (not implemented)

10. For any shortcomings, please add:


The code is implemented as follows:

The Code environment is as follows:

The system is CentOS6.3 x86_64. Both screen packages and mutual trust are installed.

Run the command: yum makecache on the following three nodes:

10.1.6.1 disk1.mos.com disk1

10.1.6.2 disk2.mos.com disk2

10.1.6.3 disk3.mos.com disk3

#!/bin/bashCMD=${1:-/usr/bin/yum makecache}CMD_SN="disk_ver.1"DATE="/bin/date +%k:%M:%S/%Y-%m-%d"SSH="/usr/bin/ssh -q -o StrictHostKeyChecking=no"SCREEN=/usr/bin/screenLIST=(`cat /etc/hosts|grep mos.com|awk '{print $1}'`)for i in ${LIST[@]};do    $SSH $i $SCREEN -dmS $CMD_SN    $SSH $i $SCREEN -S $CMD_SN -X screen $CMD    echo -en "`$DATE` $i done.\n\n"done


Place a simple script on the three nodes to check whether the command is successfully executed. The Code is as follows:

#!/bin/bashDate="/bin/date +%k:%M:%S/%Y-%m-%d"Mlog=/var/log/mon.logTEE(){        /usr/bin/tee -a $Mlog}while :;do    INFO=`ps aux|grep -v grep|grep yum`    if [ -n "$INFO" ];then        echo -en "`$Date` : $INFO \n\n" |TEE    fi    sleep 1done


Execute the script on the control node. The result is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1HT55J2-1.jpg "title =" qq 30723170944.jpg "/>


The task start time is shown as follows on the three nodes:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1HT51R9-2.jpg "title =" 2013-07-23_171148.jpg "/>



It is not easy to write text. If you think you can join the column, you will be well received ..



After finishing this process, I suddenly thought that this item should also be available in the background. It is true that it can be tested. Therefore, the code can be changed:

#!/bin/bashCMD=${1:-/usr/bin/yum makecache}DATE="/bin/date +%k:%M:%S/%Y-%m-%d"LOG=/var/log/ctrl.logSSH="/usr/bin/ssh -q -o StrictHostKeyChecking=no"LIST=(`cat /etc/hosts|grep mos.com|awk '{print $1}'`)for i in ${LIST[@]};do        $SSH $i $CMD &>> $LOG &                   echo -en "`$DATE` $i done.\n\n"done

Record all output logs to log files and use logs to determine whether execution results are correct or not. In this way, 4th and 7 of the above items can be completed, and 5th issues do not exist. There are only 6th and 9 issues to be considered.



This article from "self-improvement" blog, please be sure to keep this source http://mos1989.blog.51cto.com/4226977/1255884

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.