当前位置: 首页 > news >正文

集群节点批量执行 shell 命令

1、SSH 工具本身支持多窗口

比如 MobaXterm:
在这里插入图片描述
在这里插入图片描述

2、编写脚本通过 ssh 在多台机器批量执行shell命令

创建 ssh_hosts 配置文件,定义需要批量执行的节点(必须能够通过 ssh 免密登录,且存在同名用户)

vim ssh_hosts
bigdata0
bigdata1

创建 dosshcmd.sh 脚本:

vim dosshcmd.sh
#! /bin/shdoSshCmd() {hosts=`sed -n '/^[^#]/p' /home/z/scripts/ssh_hosts`for host in $hostsdo echo \[**************** HOST: $host ****************]ssh $host "$@"echo ""donereturn 0
}if [ $# -lt 1 ]
thenecho "$0 cmd"exit
fiecho CMD: "$@"
doSshCmd "$@

赋予执行权限

chmod u+x dosshcmd.sh

3、验证:

1) 验证简单命令:
[root@bigdata0 scripts]# ./dosshcmd.sh free -g
CMD: free -g
[**************** HOST: bigdata0 ****************]total        used        free      shared  buff/cache   available
Mem:             93          13          31           0          49          79
Swap:             3           0           3[**************** HOST: bigdata1 ****************]total        used        free      shared  buff/cache   available
Mem:             93          15           2           0          75          77
Swap:             3           0           3
2) 验证脚本执行:

编写一个通过 jps 找到 pid,然后通过 ps 查看进程信息的简单脚本:

vim find_java_resources.sh
#! /bin/shpids=`jps|grep -i "$1"| cut -d ' ' -f 1`
echo ----find pids: $pids
for pid in $pids
doecho ----PID: $pid----if [ $# -eq 1 ];then ps -q $pid -aux|awk '{print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10}'|column -t elseps -q $pid ${@:2}|awk '{print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10}'|column -tfiecho " "
done

赋予执行权限

chmod u+x find_java_resources.sh

分发:

[root@bigdata0 scripts]# scp find_java_resources.sh bigdata1:`pwd`
find_java_resources.sh                              100%  265     0.3KB/s   00:00    
[root@bigdata0 scripts]#

查看集群每个节点 yarn 任务资源占用

[root@bigdata0 scripts]# ./dosshcmd.sh /home/z/scripts/find_java_resources.sh yarn
CMD: /home/z/scripts/find_java_resources.sh yarn
[**************** HOST: bigdata0 ****************]
----find pids: 31878 31879
----PID: 31878----
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME
root     31878 24.7  1.2 26774340 1204428 ?    Sl   11:10   2:49 ----PID: 31879----
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME
root     31879 25.9  1.2 26743664 1210208 ?    Sl   11:10   2:57​
[**************** HOST: bigdata1 ****************]
----find pids: 30703
----PID: 30703----
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME
root     30703 24.4  2.9 26776844 2854336 ?    Sl   11:14   2:48 

也可以用 watch 监控

watch "./dosshcmd.sh /home/z/scripts/find_java_resources.sh yarn"
http://www.lryc.cn/news/209671.html

相关文章:

  • fl studio21.2水果软件怎么设置中文?
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • nbcio-boot如何进行gitee第三方登录
  • 【C语言】字符函数、字符串函数与内存函数
  • 生成树协议:监控 STP 端口和交换机
  • 【黑产攻防道03】利用JS参数更新检测黑产的协议破解
  • 什么是web3.0?
  • 二、W5100S/W5500+RP2040树莓派Pico<DHCP>
  • 【开源】基于SpringBoot的天然气工程业务管理系统的设计和实现
  • 讯飞星火大模型V3.0 WebApi使用
  • 拥有DOM力量的你究竟可以干什么
  • GnuTLS recv error (-110): The TLS connection was non-properly terminated
  • Notepad++安装插件和配置快捷键
  • iOS Autolayout 约束设置【顺序】的重要性!
  • Echarts渲染不报错但是没有内容
  • 数据结构 | 算法的时间复杂度和空间复杂度【详解】
  • 高级篇之ENC编码器多机位帧同步配置详解
  • matlab simulink 四旋翼跟拍无人机仿真
  • jenkins、ant、selenium、testng搭建自动化测试框架
  • 【阅读和学习代码】VoxelNet
  • 【23种设计模式】接口隔离原则
  • 【Python机器学习】零基础掌握PartialDependenceDisplay检验、检查
  • Jmeter的接口自动化测试
  • windows c++获取开机启动项
  • 【C++初阶】类和对象——构造函数析构函数拷贝构造函数
  • Java实现SQL分页
  • 软件测试进阶篇----自动化测试脚本开发
  • rust std
  • SpringMVC(下)
  • 分布式操作系统的必要性及重要性