【发布时间】:2015-11-16 16:10:33
【问题描述】:
早安!尝试使用 nohup 在多个服务器上启动脚本并使其在后台运行。
命令(由我的供应商提供)在每个服务器上直接输入时按预期运行(python 脚本会抓取日志文件并通过 UDP 将相关信息发送到另一台服务器):
nohup tail -f /log/log.log | python /test/deliver.py > /dev/null 2>&1 &
但是,当进入 for 循环以到达许多服务器时,我必须在每个服务器之间按 Ctrl-C 以保持循环继续。
如果可能,请提供帮助:
for i in `cat /etc/hosts`; do ssh $i nohup tail -f /log/log.log | python /test/deliver.py > /dev/null 2>&1 &; done
解决方案(谢谢大家的帮助):
ssh -f user@host "cd /whereever; nohup ./whatever > /dev/null 2>&1 &"
必须将 -f 与双引号结合使用,如下所述:
Getting ssh to execute a command in the background on target machine
【问题讨论】:
-
您正在本地运行
nohup tail -f /log/log.log在ssh和| python ...管道下。命令解析。单引号你想在远程主机上运行的整个命令。 -
感谢您的快速响应 Etan - 尝试了以下,相同的结果 - 脚本成功启动,但 ssh 会话挂起等待 ctrl-C 恢复:for i in mytestserver;做 ssh $i 'nohup tail -f /log/log.log | python /test/deliver.py > /dev/null 2>&1 &';完成
-
我有点惊讶
ssh在远程命令结束时没有退出,但也许你需要强制退出。您可以尝试将exit粘贴在引用命令的末尾(但在此处不起作用的快速测试中),因此您可能需要将-f参数改为ssh。同样循环/etc/hosts似乎是一个可怕的想法,因为它可能会多次访问本地主机以及每个主机。更何况你shouldn't read lines withfor。 -
谢谢,exit 或 -f 也没有运气(我有 cat /etc/hosts 的 grep 和 awk 命令,尽量保持简单,不显示公司敏感信息)
标签: bash loops for-loop background nohup