shell脚本关闭计算机集群

    xiaoxiao2025-05-25  8

    背景:由于用4台机器建立了一个Hadoop集群,每次开机还好,直接按4台,但是每次关机就比较麻烦(不想直接长按电源鍵),因为目前只有一台机器有显示器,另外三台都没有显示器,所以,每次关闭其他三台时,都需要在终端下用ssh连接到host,然后再输入关机命令,现在三台机器,稍微还好一些,如果机器再多几台,那就太麻烦了,因此,专门写了一个脚本来自动关机。

    #!/usr/bin/env bash ssh -t node1 "echo \"hadoop\" | sudo -S shutdown -h now" ssh -t node2 "echo \"hadoop\" | sudo -S shutdown -h now" ssh -t node3 "echo \"hadoop\" | sudo -S shutdown -h now"

    关机脚本代码如上,node1,node2,node3表示三台机器,ssh后面跟了一个-t,表示以一个伪终端的方式连接远程机器,并执行后续的命令。man ssh 时,-t的解释如下:

    -t Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

    因为在sudo的时候,需要输入用户密码,所以,在sudo的时候,带了一个配置项-S,通过man sudo ,可以发现:

    S, --stdin Write the prompt to the standard error and read the password from the standard input instead of using the terminal device. The password must be followed by a newline character.

    也就是说,加了这个参数后,可以从标准输入读取密码,因此,在sudo之前输出密码即可,从而,实现在sudo的时候,不需要手动输入密码。以上hadoop就是密码。

    转载请注明原文地址: https://ju.6miu.com/read-1299267.html
    最新回复(0)