linux - Bash poweroff 脚本挂起系统

标签 linux bash ubuntu unix ssh

我的意图是在我的 ip 列表中循环,如果我的 ping 首先成功,则关闭电源。但是系统似乎挂起。运行此脚本后,我无法再对系统执行 ping 操作,它们也没有关机。如果我通过终端运行 ssh 192.168.1.ip "sudo poweroff" 我不会遇到这个问题。有什么建议吗?

for ((ip=40, cnt=0; ip<=max; ip++, cnt++))
do
    if ping -c 1 192.168.1.$ip &> /dev/null
    then
        printf "\n${array[$cnt]}: Ping Successful"
        ssh 192.168.1.$ip "sudo poweroff"
        printf "\n${array[$cnt]}: Power Down Executed\n"
        sleep 1
    else
        printf "\n${array[$cnt]}: Ping Failed\n"    
    fi
done

运行单个 ssh 192.168.1.40 "sudo poweroff" 后,系统正确关闭电源。运行我的脚本时,我无法 ping 系统,但我可以直观地看到风扇和 LED 灯仍然亮着。我想我应该使用 KVM 来仔细查看,因为运行此脚本后 ssh 不允许连接。乍一看,我仍然不明白运行 ssh 192.168.1.40 "sudo poweroff" 并通过我的脚本运行它真的有什么不同。不管怎样,我明天会尝试添加更多信息。

最佳答案

ssh 192.168.1.$ip "(sleep 5; sudo poweroff)&" to put the process in the background on the remote host and sleep for 5 seconds before powering off to give time for the script to complete and exit the remote host before it goes down... – David C. Rankin

这解决了我的问题。

关于linux - Bash poweroff 脚本挂起系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47194255/

相关文章:

linux - 将 apache 配置为可通过目录/wiki 中的端口 1234 进行访问

bash - 使用 awk 解析 CSV 文件时,为什么会忽略空单元格?

docker |未知环境 `bash` |子进程/usr/bin/dpkg 返回错误代码(1)

linux - 动态加载程序如何使用正确的 arch 版本加载库

linux - 找不到 LIBBFD(缺少 : LIBBFD_BFD_LIBRARY LIBBFD_IBERTY_LIBRARY LIBBFD_OPCODES_LIBRARY LIBBFD_INCLUDE_DIRS)

linux - 如何将 avprobe 的输出重定向到文件?

bash - 为什么带有 -w (--word-regexp) 标志的 grep 如此缓慢且占用大量内存?

linux - 如何用同一文本行中的尾随字符替换字符匹配项?

linux - 启动 Jenkins 时它正在启动但是当检查 Jenkins 的状态时它说事件(退出)

linux - Perl - 如何发送本地邮件?