Linux自定义脚本整合

1、集群分发文件

应用场景

我们经常需要将新建的活着修改后的文件,循环复制文件到所有节点的相同目录下,一次一次执行scp命令显得并不那么友好。

基础知识

(a)rsync命令原始拷贝:

[root@bigdata801 hadoop-3.3.1]# rsync -av /opt/module/hadoop-3.3.1/ bigdata802:/opt/module/hadoop-3.3.1/

(b)期望脚本:

xsync 要同步的文件名称

(c)期望脚本在任何路径都能使用(脚本放在声明了全局环境变量的路径)

[root@bigdata801 bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/module/jdk1.8.0_181/bin:/opt/module/hadoop-3.3.1/bin:/opt/module/hadoop-
3.3.1/sbin:/root/bin:/opt/module/jdk1.8.0_181/bin:/opt/module/hadoop-
3.3.1/bin:/opt/module/hadoop-3.3.1/sbin:/opt/module/jdk1.8.0_181/bin:/opt/module/hadoop-
3.3.1/bin:/opt/module/hadoop-3.3.1/sbin:/opt/module/jdk1.8.0_181/bin:/opt/module/hadoop-
3.3.1/bin:/opt/module/hadoop-3.3.1/sbin
脚本实现

(a)在/root/bin目录下创建xsync文件

[root@bigdata801 ~]# cd /root/
[root@bigdata801 ~]# mkdir bin
[root@bigdata801 ~]# cd bin/
[root@bigdata801 bin]# vim xsync

在该文件中编写如下代码

#!/bin/bash

#1. 判断参数个数
if [ $# -lt 1 ]
then
    echo Not Enough Arguement!
    exit;
fi

#2. 遍历集群所有机器
for host in bigdata801 bigdata802 bigdata803 bigdata804
do
    echo ====================  $host  ====================
    #3. 遍历所有目录,挨个发送

    for file in $@
    do
        #4. 判断文件是否存在
        if [ -e $file ]
            then
                #5. 获取父目录
                pdir=$(cd -P $(dirname $file); pwd)
                #6. 获取当前文件的名称
                fname=$(basename $file)
                ssh $host "mkdir -p $pdir"
                rsync -av $pdir/$fname $host:$pdir
            else
                echo $file does not exists!
        fi
    done
done
修改脚本 xsync 具有执行权限
[root@bigdata801 bin]# chmod 777 xsync 
[root@bigdata801 bin]# ll
总用量 4
-rwxrwxrwx 1 root root 753 822 20:45 xsync
测试脚本

将刚创建的 bin 分发到其他机器

[root@bigdata801 ~]# xsync bin/

执行过程如下

[root@bigdata801 ~]# xsync bin/
==================== bigdata801 ====================
sending incremental file list

sent 75 bytes  received 17 bytes  184.00 bytes/sec
total size is 753  speedup is 8.18
==================== bigdata802 ====================
sending incremental file list
bin/
bin/xsync

sent 878 bytes  received 39 bytes  611.33 bytes/sec
total size is 753  speedup is 0.82
==================== bigdata803 ====================
sending incremental file list
bin/
bin/xsync

sent 878 bytes  received 39 bytes  1,834.00 bytes/sec
total size is 753  speedup is 0.82
==================== bigdata804 ====================
sending incremental file list
bin/
bin/xsync

sent 878 bytes  received 39 bytes  611.33 bytes/sec
total size is 753  speedup is 0.82
[root@bigdata801 ~]# 

在 bigdata802上查看文件是否分发成功

[root@bigdata802 hadoop-3.3.1]# cd /root/
[root@bigdata802 ~]# ll
总用量 4
-rw-------. 1 root root 1460 228 19:08 anaconda-ks.cfg
drwxr-xr-x. 2 root root   19 822 20:47 bin
[root@bigdata802 ~]# 

2、在某个节点上查看所有节点的状态(JPS)

应用场景

启动集群以后,我们需要查看集群是否启动成功的关键一步就是在各个服务上输入JSP命令查看该服务器上对应的服务,但是每次在各个机器上输一遍未免太麻烦,所以编写以下脚本。

脚本实现

在 /root/bin 下创建 jpsall 文件

[root@bigdata801 bin]# cd /root/bin/
[root@bigdata801 bin]# vim jpsall

脚本内容如下

#!/bin/bash
  
#执行 JPS 命令,查询每台服务器上的节点状态

echo ===============查询每台服务器状态===============

for host in bigdata801 bigdata802 bigdata803 bigdata804
do
    echo =============== $host ===============

    ssh $host '/opt/module/jdk1.8.0_181/bin/jps'
done

echo ===============查询服务器状态结束===============
授予脚本执行权限
[root@bigdata801 bin]# chmod +x jpsall
分发/root/bin/目录

保证自定义脚本在每台机器上都可以使用

[root@bigdata801 bin]# xsync /root/bin/
测试
[root@bigdata801 bin]# jpsall 
===============查询每台服务器状态===============
=============== bigdata801 ===============
5712 JobHistoryServer
5202 DataNode
5542 NodeManager
5047 NameNode
5967 Jps
=============== bigdata802 ===============
3156 NodeManager
3461 Jps
3064 SecondaryNameNode
2943 DataNode
=============== bigdata803 ===============
3488 ResourceManager
3281 DataNode
3651 NodeManager
4139 Jps
=============== bigdata804 ===============
2576 DataNode
2699 NodeManager
2957 Jps
===============查询服务器状态结束===============
[root@bigdata801 bin]# 

3、在多台机器执行统一命令

应用场景

在我们修改环境变量以后,需要重新加载source一下,每个节点都去执行也不那么友好

脚本实现

在 /root/bin 文件夹下创建 callall 文件

[root@bigdata801 bin]# pwd/root/bin[root@bigdata801 bin]# vim callall

脚本内容如下

#!/bin/bash
#在集群的所有机器上批量执行同一条命令
if(($#==0))
then
        echo 请输入您要操作的命令!
        exit
fi

echo 要执行的命令是:$*

#循环执行此命令
for((i=801;i<=804;i++))
do
        echo ---------------------bigdata$i-----------------
        ssh bigdata$i $*
done
授予可执行权限
[root@bigdata801 bin]# chmod 777 callall
分发其他机器
[root@bigdata801 bin]# xsync /root/bin/
==================== bigdata801 ====================
sending incremental file list

sent 150 bytes  received 17 bytes  334.00 bytes/sec
total size is 2,556  speedup is 15.31
==================== bigdata802 ====================
sending incremental file list
bin/
bin/callall

sent 489 bytes  received 39 bytes  1,056.00 bytes/sec
total size is 2,556  speedup is 4.84
==================== bigdata803 ====================
sending incremental file list
bin/
bin/callall

sent 489 bytes  received 39 bytes  352.00 bytes/sec
total size is 2,556  speedup is 4.84
==================== bigdata804 ====================
sending incremental file list
bin/
bin/callall

sent 489 bytes  received 39 bytes  1,056.00 bytes/sec
total size is 2,556  speedup is 4.84
[root@bigdata801 bin]# 
测试
[root@bigdata801 bin]# callall ls -l
要执行的命令是ls -l
---------------------bigdata801-----------------
总用量 4
-rw-------. 1 root root 1460 228 19:08 anaconda-ks.cfg
drwxr-xr-x  2 root root   67 829 14:11 bin
---------------------bigdata802-----------------
总用量 4
-rw-------. 1 root root 1460 228 19:08 anaconda-ks.cfg
drwxr-xr-x. 2 root root   67 829 14:11 bin
---------------------bigdata803-----------------
总用量 4
-rw-------. 1 root root 1460 228 19:08 anaconda-ks.cfg
drwxr-xr-x. 2 root root   67 829 14:11 bin
---------------------bigdata804-----------------
总用量 4
-rw-------. 1 root root 1460 228 19:08 anaconda-ks.cfg
drwxr-xr-x. 2 root root   67 829 14:11 bin
[root@bigdata801 bin]# 
注意

如果输入 callall ll 报错 bash: ll: 未找到命令

是因为"ll"命令不是linux的基本命令,它是"ls -l"的别名,部分版本并不直接支持“ll”命令输出。

[root@bigdata801 bin]# callall ll
要执行的命令是ll
---------------------bigdata801-----------------
bash: ll: 未找到命令
---------------------bigdata802-----------------
bash: ll: 未找到命令
---------------------bigdata803-----------------
bash: ll: 未找到命令
---------------------bigdata804-----------------
bash: ll: 未找到命令
[root@bigdata801 bin]# 

猜你喜欢

转载自blog.csdn.net/weixin_35353187/article/details/120621887