Near concurrent shell remote invocation thinking and partial implementation via screen

Source: Internet
Author: User
Tags error handling execution ssh

Suppose a scenario that requires the same command to be executed at multiple nodes (considering the uncertainty of the command), typically using a for loop, called directly by SSH. Example: You need to see the root usage of each node:

In fact, just write a few lines of these commands into a script, the command to be executed as a positional parameter can meet these requirements in a cluster of mutual trust, seemingly the problem has been resolved, the author has done this before, but the problem arises, some of the implementation of the process is long, and this sequential implementation of the method, When some commands are executed, the waiting time is absolutely collapsing, for example, if you update the library (yum update) for each node in a cluster of hundreds of nodes, or generate a yum cache (yum Makecache), or each node is new, there are a lot of disks that need to be formatted to mount, If you use a for loop to execute sequentially, trust me, the waiting time for the formatted disk will be unbearable if the task is to be completed at a specified time ....

Well, long-winded half-day, the demand appears, first think to solve the problem, after a simple partial implementation and testing

1, all nodes are mutual trust, through the installation system implementation (System installation phase implementation)

2, all nodes can use regular regular expressions to remove the corresponding IP list (script implementation)

3, must have installed the screen package, by calling this command to achieve near concurrent execution of the task (script implementation)

4. The return value of the command execution result is obtained to determine whether the execution was successful (not implemented)

5. Screen's resource release problem (not implemented)

6. Consider error handling of network failure (not implemented)

7, consider the failure of the implementation of the processing (not implemented)

8. If you need to perform too many tasks at each node, consider writing a simple execution script for the tasks you need to perform, push, and control execution, and the implementation of 4, 5, 6, 7 can consider this method.

9. Consider the problem of environment variables when executing commands remotely (not implemented)

10, deficiencies, welcome to add:

The Code section is implemented as follows:

The code environment is as follows:

System for CentOS6.3 x86_64 have installed screen pack and trust

At the node of mutual trust the command: Yum Makecache is executed at 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/bash
Cmd=${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/screen
list= (' Cat/etc/hosts|grep Mos.com|awk ' {print} ') for
I in ${list[@]};d o
    $SSH $ I $SCREEN-dms $CMD _sn
    $SSH $i $SCREEN-S $CMD _sn-x screen $CMD
    echo-en "' $DATE ' $i done.\n\n" done

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

Place a simple script on three nodes to see if the command was executed successfully, as follows:

#!/bin/bash
date= "/bin/date +%k:%m:%s/%y-%m-%d"
mlog=/var/log/mon.log
TEE () {
        /usr/bin/tee-a $  Mlog
} while
:;d o
    info= ' ps aux|grep-v grep|grep yum '
    if [-N ' $INFO '];then
        echo-en ' $Date ': $INFO \ n ' | TEE
    fi
    1 done

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.