Linux脚本记录(不定时更新中)

记录个人学习中使用的Linux脚本,备忘!

集群分发脚本

循环复制文件到所有节点的相同目录下

rsync命令原始拷贝:

1
rsync -av /opt/module root@hadoop103:/opt/

期望脚本:

xsync [要同步的文件名称]

① 在用户的家目录/home/itjing下创建bin文件夹

1
2
# 说明:在/home/itjing/bin这个目录下存放的脚本,itjing用户可以在系统任何地方直接执行。
[itjing@hadoop102 ~]$ mkdir bin

② 在/home/itjing/bin目录下创建xsync文件,以便全局调用

1
2
3
[itjing@hadoop102 ~]$ cd /home/itjing/bin

[itjing@hadoop102 bin]$ vim xsync

在该文件中编写如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
# 1. 判断参数个数
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi

#2. 遍历集群所有机器
for host in hadoop102 hadoop103 hadoop104
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具有执行权限

1
[itjing@hadoop102 bin]$ chmod +x xsync

④ 测试脚本

1
2
# 把自己分发出去
[itjing@hadoop102 bin]$ xsync xsync

集群所有进程查看脚本

在 /home/itjing/bin 目录下创建脚本 xcall.sh

1
vim xcall.sh

在脚本中编写如下内容

1
2
3
4
5
6
#! /bin/bash
for i in hadoop102 hadoop103 hadoop104
do
echo --------- $i ----------
ssh $i "$*"
done

修改脚本执行权限

1
chmod 777 xcall.sh

启动脚本

1
xcall.sh jps
点击查看

本文标题:Linux脚本记录(不定时更新中)

文章作者:LiJing

发布时间:2023年08月13日 - 13:47:37

最后更新:2023年11月25日 - 14:35:33

原始链接:https://blog-next.xiaojingge.com/posts/1686234425.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------