Batch execution of the same command shell script on a Linux cluster __linux

Source: Internet
Author: User
Tags ssh scp command
Execute the same command on multiple machines on a cluster because of actual development needs. For example, execute a specified script, delete files in the same directory (this is prudent ~ ~) and so on, the members of the group developed a ALLSCP (can execute the SCP command to synchronize the online code) very good, I also wrote a, The main is to use SSH to log on to a server, and then execute a specified command.

First paste the shell code:

#!/bin/bash
If ["$#"-ne 2]; then
    echo "USAGE: $0-f server_list_file cmd"
    exit-1
fi

file_name=$1< C5/>cmd_str=$2

cwd=$ (PWD)
CD $cwd
serverlist_file= "$CWD/$file _name"

if [!-e $serverlist _file]; Then
    Echo ' server.list not exist ';
    Exit 0
Fi while read line do
    #echo $line
    If [-N "$line"]; then
        echo "Doing--->>>  ;>> "$line" <<<<<<< "
        ssh $line $cmd _str </dev/null >/dev/null
        if [$-eq 0] ; Then
            echo "$cmd _str done!"
        else
            echo "error:" $?
        Fi
    fi done
< $serverlist _file

The code is simple and does not explain.

How to use:

1. Create a new file host_file_list, file for the server's address, each row;

2. Save the above shell script, such as Save as allcmd.sh, pay attention to using chmod +x allcmd.sh make it executable script;

3. Run allcmd.sh host_file_list MD, Host_file_list is the 1th step of the file name (remember and allcmd.sh placed in the same directory), CMD is to execute the command, wrapped in single quotes, such as: Delete/home /nuaazdh/a tmp.txt file below: allcmd.sh host_file_list ' Rm/home/nuaazdh/tmp.txt '

4. 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.